gpt4 book ai didi

ios - 约束会影响哪些属性?

转载 作者:搜寻专家 更新时间:2023-11-01 06:22:22 24 4
gpt4 key购买 nike

我正在尝试理解约束的逻辑,但有些事情我无法找到...

我创建了一个自定义 UIButton 并添加了一些约束来更改它的宽度和高度,并且它工作正常。但是我找不到按钮的新宽度和高度值。约束更改后如何获取这些值?

这是我的代码...

import Foundation
import UIKit

class RoundedButton: UIButton {

class func make (text:String, color:UIColor) -> RoundedButton {

let button = self.buttonWithType(UIButtonType.System) as RoundedButton
button.frame = CGRectMake(0, 0, 150, 40)
button.layer.cornerRadius = 20
button.layer.borderColor = color.CGColor
button.layer.borderWidth = 2

button.setTitle(text, forState: UIControlState.Normal)
button.setTitleColor(color, forState: UIControlState.Normal)
button.titleLabel?.font = UIFont(name: "OpenSans-Semibold", size: 14)
return button
}

func resizeAndPlace(width:CGFloat, height:CGFloat)
{
self.setTranslatesAutoresizingMaskIntoConstraints(false)

let widthCons = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: width)

let heightCons = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: height)

self.superview!.addConstraint(widthCons)
self.superview!.addConstraint(heightCons)

println(self.frame.width) // 150 and never changes
}
}

最佳答案

您没有看到约束生效的原因是布局尚未发生。如果你打电话:

self.layoutIfNeeded()

设置约束后,值将立即反射(reflect)在框架中。

关于ios - 约束会影响哪些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31701880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com