Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Auto Layout constraints programatically to a subview of constant width and height
    primarykey
    data
    text
    <p>I have a UIView of size 20x40 which has been added as a subview of a ViewController. The smaller UIView is draggable across the screen and depending on the position it has been dragged to, the UIView will reset the 'X' coordinate to stick to the screen edge.</p> <p>However, I want to use/learn 'Auto Layout' to add constraints to this subView to maintain the same width and height in both landscape and portrait mode.</p> <p>Code so far:</p> <pre><code>self.anotherView = [[UIView alloc] initWithFrame: CGRectMake (0.0,(self.view.frame.size.height/2)-45.0f-self.navigationController.navigationBar.frame.size.height,20.0,45.0f)]; UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragging:)]; [self.anotherView addGestureRecognizer:panRecognizer]; [self.anotherView setBackgroundColor: [UIColor blueColor]]; self.anotherView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview: self.anotherView]; [self.view bringSubviewToFront:self.anotherView]; NSDictionary *views = @{@"subview" : self.anotherView, @"superview" : self.view}; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview(==20)]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:views]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subview(==40)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:views]]; NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.anotherView attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.view.bounds.size.height]; [self.view addConstraint:constraint]; </code></pre> <p>However, the above code gives me the following error:</p> <pre><code>Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "&lt;NSLayoutConstraint:0x8b4ef80 H:|-(0)-[UIView:0x8b4eab0] (Names: '|':UIView:0x8b4cc80 )&gt;", "&lt;NSLayoutConstraint:0x8b4f020 H:[UIView:0x8b4eab0(20)]&gt;", "&lt;NSLayoutConstraint:0x8b4f0f0 H:[UIView:0x8b4eab0]-(0)-| (Names: '|':UIView:0x8b4cc80 )&gt;", "&lt;NSAutoresizingMaskLayoutConstraint:0x8aa1d30 h=--&amp; v=--&amp; H:[UIView:0x8b4cc80(320)]&gt;" ) Will attempt to recover by breaking constraint &lt;NSLayoutConstraint:0x8b4f0f0 H:[UIView:0x8b4eab0]-(0)-| (Names: '|':UIView:0x8b4cc80 )&gt; Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in &lt;UIKit/UIView.h&gt; may also be helpful. </code></pre> <p>What exactly am I doing wrong here? Also, I am using Storyboard and only this view is added as the subView to the ViewController. Please find below the screenshot of how the UI looks at present.</p> <p><img src="https://i.stack.imgur.com/9mol6.png" alt="The blue view is freely draggable across the screen!"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload