gpt4 book ai didi

ios - 使用 constraintsWithVisualFormat 的自动布局效果不佳

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

我已设置 ScrollView 并向 ScrollView 添加约束。但它显示空白屏幕。请检查以下代码。

override func loadView()
{
super.loadView()
scrollView = UIScrollView(frame:CGRectZero)
scrollView.backgroundColor = UIColor.whiteColor()

scrollView.sizeToFit()

self.view.addSubview(scrollView)
scrollView.backgroundColor = UIColor.blueColor()

contentView = UIView()
// contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.backgroundColor = UIColor.redColor()
scrollView.addSubview(contentView)

var viewBindingsDictBoth = [String: AnyObject]()
viewBindingsDictBoth["scrollView"] = scrollView
viewBindingsDictBoth["contentView"] = contentView
viewBindingsDictBoth["mainView"] = self.view


view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|",options: [], metrics: nil, views:viewBindingsDictBoth))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView]-0-|",options: [], metrics: nil, views:viewBindingsDictBoth))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|",options: [], metrics: nil, views:viewBindingsDictBoth))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[contentView]|",options: [], metrics: nil, views:viewBindingsDictBoth))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[contentView(==mainView)]",options: [], metrics: nil, views:viewBindingsDictBoth))

self.view.contentMode = UIViewContentMode.Redraw
}

提前致谢..

最佳答案

你最想念的是

scrollView.translatesAutoresizingMaskIntoConstraints = false

contentView.translatesAutoresizingMaskIntoConstraints = false

我认为您的某些约束并不完美,但最大的问题是每个 View 都默认使用其自动调整属性进行定位。这些将与您的约束发生冲突。

来自 UIView.translatesAutoresizingMaskIntoConstraints documentation :

Note that the autoresizing mask constraints fully specify the view’s size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts. If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to false, and then provide a non ambiguous, nonconflicting set of constraints for the view.

(强调我的)

关于ios - 使用 constraintsWithVisualFormat 的自动布局效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37570705/

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