简体   繁体   English

如何添加具有约束的子视图以使子视图具有与其父视图相同的框架?

[英]How to add a subview with constraints to make the subview with the same frame as its superview?

Within UIView I have a method presentView() : UIView我有一个方法presentView()

public func presentView() {

    UIApplication.sharedApplication().delegate?.window??.addSubview(self)

    let blurEffect = UIBlurEffect(style: .Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.backgroundColor = UIColor.yellowColor()
    addSubview(blurEffectView)

    let topConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 0)
    let bottomConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0)
    let leadingConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
    let trailingConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)

    self.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])

    print("newframe: \(blurEffectView.frame)")

}

but sth is wrong because this is the output on console: 但这是错误的,因为这是控制台上的输出:

newframe: (0.0, 0.0, 0.0, 0.0) newframe:(0.0,0.0,0.0,0.0)

and result is: 结果是:

在此处输入图片说明

Remember about: 记住:

blurEffectView.translatesAutoresizingMaskIntoConstraints = false

and then: 接着:

let topConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 0)

addSubview(blurEffectView)
addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
layoutIfNeeded()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使超级视图自动调整大小并与其子视图保持一致? - How to make a superview resize automatically same size and coordinate with its subview? 如果子视图的大小与其父视图相同,如何使用ctrl-drag在IB中设置约束? - How to set constraints in IB using ctrl-drag if the size of my subview is the same as its superview? 如何以编程方式设置子视图的框架以使其适合其父视图? - How to programmatically set a subview's frame to fit it inside its superview? 触摸其父视图框架之外的子视图 - Touches on a subview outside the frame of its superview 移动子视图时,如何在其超视图中将其子视图保留在同一位置? - When moving a subview around, how do I keep a subview of it in the same place in its superview? 添加为超级视图一半的子视图 - Add a subview that is half the superview 如何将子类添加为子视图,以便在superview前面设置子视图的背景 - How to add a subclass as a subview so that background of subview is set in front of superview 如何让子视图使用视觉格式约束来填充其父视图? - How to let subview fill up its superview using visual format constraints? 如何在Swift中使Xib子视图与超级视图交互? - How to make a xib subview interact with superview in Swift? 如何从超级视图的超级视图中将subView置于顶部 - How to bring subView to top from superview of its superview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM