作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已设置 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/
我是一名优秀的程序员,十分优秀!