gpt4 book ai didi

ios - 来自 CGRectMake 的 UIView 没有正确的高度

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

我正在以编程方式将 UIView 添加到容器 View (但是,容器 View 是在 Storyboard 中创建的)。这是代码:

class ViewController: UIViewController{
@IBOutlet weak var dwView: UIView!
private var dwSelector = dwSelectorView()
override func viewDidLoad(){
super.viewDidLoad()
addDWSelector()
}

func addDWSelector(){
dwSelector.setTranslatesAutoresizingMaskIntoConstraints(false)
dwSelector.frame = CGRectMake(self.dwView.bounds.origin.x, self.dwView.bounds.origin.y, self.dwView.bounds.width / 2.0, self.dwView.frame.height)
println("dw height: \(self.dwView.frame.height)")
//prints 568, way too large of a value
self.dwView.addSubview(dwSelector)
}
}

dwView 的高度在 Storyboard 中是 123 但打印状态打印的是 568 所以现在它看起来像这样:

one

最佳答案

你不应该总是依赖 -(void)viewDidLoad 因为此时 View 边界不正确或者 - (void)viewWillAppear 如果你使用自动布局来设置你的观点的框架。如果你在 UIViewController 中布局 View ,viewDidLayoutSubviews() 是一个合适的地方,如果你在 UIView 中布局 subview ,它是 layoutSubviews()

查看这篇文章以获取更多详细信息:Where to progmatically lay out views in iOS 5 (and handling orientation changes)

关于ios - 来自 CGRectMake 的 UIView 没有正确的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510449/

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