【发布时间】:2017-12-11 07:20:49
【问题描述】:
【问题讨论】:
标签: ios swift swift4 nslayoutconstraint ios-autolayout
【问题讨论】:
标签: ios swift swift4 nslayoutconstraint ios-autolayout
试试这个 你会发现一些有用的东西 只需添加此 UIView 扩展程序
extension UIView {
func fillInSuperView(inset:UIEdgeInsets = UIEdgeInsets.zero) {
self.translatesAutoresizingMaskIntoConstraints = false
self.leadingAnchor.constraint(equalTo: (self.superview?.leadingAnchor)!, constant: inset.left).isActive = true;
self.trailingAnchor.constraint(equalTo: (self.superview?.trailingAnchor)!, constant: -inset.right).isActive = true
self.topAnchor.constraint(equalTo: (self.superview?.topAnchor)!, constant: inset.top).isActive = true
self.bottomAnchor.constraint(equalTo: (self.superview?.bottomAnchor)!, constant: -inset.bottom).isActive = true
}
}
调用这个函数
YourView.fillInSuperView(inset: UIEdgeInsets.zero)
您可以通过设置 insets 从 superView 设置填充
【讨论】:
将 v1 框架布局更改为全屏布局。高度和宽度将是全屏高度和宽度。您可以使用此更新帧大小
let v1 = UIView(frame: CGRect(x:0, y:0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
通过使用它不需要使用Autolayout 约束。
【讨论】:
改变 v1 宽度和高度 到父宽度和高度 喜欢
let v1 = UIView(frame: CGRect(x:0, y:0, width: view.bounds.size.width, height: view.bounds.size.height))
这可能有效
【讨论】:
您的错误是您为 v1 指定了固定宽度和高度
i.e. let v1 = UIView(frame: CGRect(x:0, y:0, width: 100, height: 50))
试试这个
let v1 = UIView()
// this will take frame(x, y, width, height) from superview
v1.frame = view.frame
【讨论】:
使用此功能将 v1 帧大小更改为全视图
let v1 = UIView(frame: UIScreen.main.bounds)
这会起作用。
【讨论】:
试试这个,
let v1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
v1.backgroundColor = UIColor.gray
v1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(v1)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v1]|", options: [], metrics: nil, views: ["v1" : v1]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v1]|", options: [], metrics: nil, views: ["v1" : v1]))
【讨论】: