Width and Height Equal for its supervisor with auto-detection software?

I searched a lot of fragments on the net and I still can not find the answer to my problem. My question is: I have scrollView (SV) and I want to add a button inside scrollView (SV) programmatically with the same width and height of my supervisor, which is scrollView (SV), so that when the user rotates the device button, will have one same scrollView frame (SV). How to make NSLayout / NSLayoutConstraint? thank

+68
ios dynamic autolayout nslayoutconstraint
Sep 12 '13 at 6:13
source share
9 answers

I'm not sure if this is the most efficient way to do this, but it works.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.translatesAutoresizingMaskIntoConstraints = NO; // initialize [coverForScrolView addSubview:button]; NSLayoutConstraint *width =[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:0 toItem:coverForScrolView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]; NSLayoutConstraint *height =[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:0 toItem:coverForScrolView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]; NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:coverForScrolView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.f]; NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:coverForScrolView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f]; [coverForScrolView addConstraint:width]; [coverForScrolView addConstraint:height]; [coverForScrolView addConstraint:top]; [coverForScrolView addConstraint:leading]; 
+65
Sep 13 '13 at 8:10
source share

If someone is looking for a Swift solution - I would create a Swift extension for UIView that will help you every time you want to bind the subviews frame to its viewing restrictions:

Swift 2:

 extension UIView { /// Adds constraints to this `UIView` instances `superview` object to make sure this always has the same size as the superview. /// Please note that this has no effect if its `superview` is `nil` – add this `UIView` instance as a subview before calling this. func bindFrameToSuperviewBounds() { guard let superview = self.superview else { print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") return } self.translatesAutoresizingMaskIntoConstraints = false superview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["subview": self])) superview.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["subview": self])) } } 

Swift 3:

 extension UIView { /// Adds constraints to this `UIView` instances `superview` object to make sure this always has the same size as the superview. /// Please note that this has no effect if its `superview` is `nil` – add this `UIView` instance as a subview before calling this. func bindFrameToSuperviewBounds() { guard let superview = self.superview else { print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") return } self.translatesAutoresizingMaskIntoConstraints = false superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) } } 

Then just name it like this :

 // after adding as a subview, eg `view.addSubview(subview)` subview.bindFrameToSuperviewBounds() 
+84
Sep 28 '15 at 13:45
source share

This link may help you, follow the instructions: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

EDIT:

use the following code snippet in which subview is your subpoint.

 [subview setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[subview]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(subview)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[subview]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(subview)]]; 
+44
Sep 12 '13 at 8:26
source share

addConstraint and removeConstraint methods for UIView will become obsolete, so you should use the "convenience of creating constraints":

 view.topAnchor.constraint(equalTo: superView.topAnchor, constant: 0).isActive = true view.bottomAnchor.constraint(equalTo: superView.bottomAnchor, constant: 0).isActive = true view.leadingAnchor.constraint(equalTo: superView.leadingAnchor, constant: 0).isActive = true view.trailingAnchor.constraint(equalTo: superView.trailingAnchor, constant: 0).isActive = true 
+6
Mar 27 '17 at 9:35
source share

Swift 3:

 import UIKit extension UIView { func bindFrameToSuperviewBounds() { guard let superview = self.superview else { print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") return } self.translatesAutoresizingMaskIntoConstraints = false superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) } } 
+5
Sep 17 '16 at 16:44
source share

Approach # 1: UIView Extension

Here's a more functional Swift 3 approach with a precondition instead of print (which can easily die in the console). This will report programmer errors as failed builds.

Add this extension to your project:

 extension UIView { /// Adds constraints to the superview so that this view has same size and position. /// Note: This fails the build if the `superview` is `nil` – add it as a subview before calling this. func bindEdgesToSuperview() { guard let superview = superview else { preconditionFailure("`superview` was nil – call `addSubview(view: UIView)` before calling `bindEdgesToSuperview()` to fix this.") } translatesAutoresizingMaskIntoConstraints = false ["H:|-0-[subview]-0-|", "V:|-0-[subview]-0-|"].forEach { visualFormat in superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: visualFormat, options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) } } } 

Now just name it like this:

 // after adding as a subview, eg `view.addSubview(subview)` subview.bindEdgesToSuperview() 

Please note that the above method is already integrated into my HandyUIKit , which also adds some more convenient UIs to your project.




Approach # 2: Using Structure

If you work a lot with software restrictions in your project, I recommend that you check SnapKit.This makes working with restrictions much easier and less error prone.

Follow the instructions in the docs to include SnapKit in your project. Then import to the top of your Swift file:

 import SnapKit 

Now you can achieve the same goal with just this:

 subview.snp.makeConstraints { make in make.edges.equalToSuperview() } 
+2
Dec 30 '16 at 9:24
source share

Swift 4 using NSLayoutConstraint :

 footerBoardImageView.translatesAutoresizingMaskIntoConstraints = false let widthConstraint = NSLayoutConstraint(item: yourview, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0) let heightConstraint = NSLayoutConstraint(item: yourview, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 0) superview.addConstraints([widthConstraint, heightConstraint]) 
+1
Dec 06 '17 at 17:09 on
source share

As an additional answer, and for those who are not opposed to including third-party libraries, the PureLayout library provides a method that it is. Once the library is installed, it is as simple as

 myView.autoPinEdgesToSuperviewEdges() 

There are other libraries that can provide similar functionality, depending on taste, for example. Masonry , Cartography .

0
Sep 13 '16 at 3:57
source share

As a continuation of @Dschee's solution, SWIFT 3.0 syntax: (Note: this is not my solution , I just installed it for Swift 3.0)

 extension UIView { /// Adds constraints to this `UIView` instances `superview` object to make sure this always has the same size as the superview. /// Please note that this has no effect if its `superview` is `nil` – add this `UIView` instance as a subview before calling this. func bindFrameToSuperviewBounds() { guard let superview = self.superview else { print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") return } self.translatesAutoresizingMaskIntoConstraints = false superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) } 
0
Nov 18 '16 at 15:30
source share



All Articles