以编程方式约束 Dynamic ViewController 中的自定义 UIView
Posted
技术标签:
【中文标题】以编程方式约束 Dynamic ViewController 中的自定义 UIView【英文标题】:Constraints programmatically for custom UIView in Dynamic ViewController 【发布时间】:2017-11-22 12:36:01 【问题描述】:我正在使用此代码动态创建新的UIViewController
@IBAction func newVCBtnPressed(_ sender: Any)
let controller = DynamicVC()
show(controller, sender: sender)
在新的UIViewController
中,我使用此代码来创建新的UIView
:
override func loadView()
view = UIView()
view.backgroundColor = .lightGray
结果我有view
和.lightGray
背景色。
我想添加自定义 UIView 并以编程方式设置约束,结果我希望 UIView 具有以下约束:
顶部:0
底部:(view.frame.height*0.9)
领先:0
尾随:(view.frame.width*0.15)
宽度:(view.frame.width*0.85)
高度:(view.frame.height*0.1)
例子:
这是我的代码:
topMenuView = UIView()
topMenuView.backgroundColor = .red
view.addSubview(topMenuView)
topMenuView.translatesAutoresizingMaskIntoConstraints = false
setupConstraints(item: topMenuView, topC: 0, topToItem: view, bottomC: (view.frame.height*0.9), bottomToItem: view, widthC: (view.frame.width*0.85), heightC: (view.frame.height*0.1), leadingCon: 0, trailingCon: (view.frame.width*0.15))
我将这个构造函数用于约束:
func setupConstraints(item:UIView, topC:CGFloat, topToItem:UIView, bottomC:CGFloat, bottomToItem:UIView, widthC:CGFloat, heightC:CGFloat, leadingCon:CGFloat, trailingCon:CGFloat)
let topConstraint = NSLayoutConstraint(item: item, attribute: .top, relatedBy: .equal, toItem: topToItem, attribute: .bottom, multiplier: 1, constant: topC)
let bottomConstraint = NSLayoutConstraint(item: item, attribute: .bottom, relatedBy: .equal, toItem: bottomToItem, attribute: .top, multiplier: 1, constant: bottomC)
let widthConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: widthC)
let heightConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: heightC)
let leading = NSLayoutConstraint(item: item,attribute: .leading,relatedBy: .equal, toItem: view, attribute: .leadingMargin, multiplier: 1.0, constant: leadingCon)
let trailing = NSLayoutConstraint(item: item,attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailingMargin,multiplier: 1.0,constant: trailingCon)
view?.addConstraints([topConstraint, bottomConstraint, widthConstraint, heightConstraint, leading, trailing])
NSLayoutConstraint.activate([topConstraint, bottomConstraint, widthConstraint, heightConstraint, leading, trailing])
但结果我只收到灰色背景的 UIView,没有出现带有红色背景的新 UIView。
我做错了什么???
【问题讨论】:
一件事:只有激活约束。您不应该将它们添加到视图中。 将一个视图的 .top 与另一个视图的 .bottom 相关联也很奇怪,反之亦然。为什么不 .top 到 .top 和 .bottom 到 .bottom? 另外,在你重写的 loadView 方法中调用 super.loadView()。 【参考方案1】:你应该只指定底部或高度和宽度或尾随,否则你会在这里遇到冲突。
看游乐场:
import PlaygroundSupport
import UIKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
let red = UIView()
red.backgroundColor = .red
view.addSubview(red)
red.translatesAutoresizingMaskIntoConstraints = false
red.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
red.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
red.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.85).isActive = true
red.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1).isActive = true
PlaygroundPage.current.liveView = ViewController()
【讨论】:
以上是关于以编程方式约束 Dynamic ViewController 中的自定义 UIView的主要内容,如果未能解决你的问题,请参考以下文章