FAQ Database Discussion Community


Change Auto Constraint height of UIView

ios,objective-c,uitableview,uiview,nslayoutconstraint
I have a UIView in a UITableViewCell. in the view, I have a UITextView and a UIView. They are both auto constraint on all 4 sides. The UIView has a height constraint. I would like to update the height constraint prorrammatically. I made a outlet of the height constraint so...

UIView doesn't size itself to superView

ios,objective-c,autolayout,nslayoutconstraint
I have a UIView with constraints like this: All vertical and horizontal spaces are set to 0. Then in viewDidLoad I added a view as the subview of the original UIView. Here is what it looks like: self.innerView = [[UIView all] initWithFrame:self.myView.frame]; [self.myView addSubview:self.innerView]; When I run the app, innerView...

NSLayoutConstraint conflict. UIView-Encapsulated-Layout-Height

ios,uitableview,uiview,autolayout,nslayoutconstraint
I am having some problem with autoLayout. Particularly in iOS8. I have already read the net and found similar issues when usingUITableViewCell. But here I am not usingUITableViewCell and have noUITableView at all. When it happens I only have aADBannerView and aUIWebView to deal with. I get the following message...

Unable to simultaneously satisfy constraints in UITableView

ios,uitableview,nslayoutconstraint
In UiTableView -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"cell"; LZHThreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[LZHThreadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } [cell configureThread:self.threads[indexPath.row]]; return cell; } and in UITableViewCell -(id)configureThread:(LZHThread *)thread{ [_avatarImageView...

NSLayoutConstraint “Unable to simultaneously satisfy constraints” Errors and how to Interpret them

ios,objective-c,swift,nslayoutconstraint
I am having some issues with constraints which I can't get my head around. I think I would find it really helpful if I understood the error messages (specifically relating to NSLayoutConstraints) in the console window better. Here is an example: I have this constraint: NSLayoutConstraint(item: mainView, attribute: NSLayoutAttribute.Height, relatedBy:...

Layout constraint to resize image to screen width maintaining aspect ratio

ios,uiscrollview,autolayout,resolution,nslayoutconstraint
I have a background image which is to be the same width as the iPhone screen but longer in length. I want to scroll the image along the y axis. I have set up a scroll view and placed an image inside which works fine as I can scroll to...

Change NSLayoutConstraint within UIView Subclass

ios,objective-c,nslayoutconstraint
I am trying to affect the NSLayoutConstraint within my UIView subclass. However where ever I put the code it doesn't seem to change the autolayout constraint. I have used NSLayoutConstraint many times before but for some reason cannot seem to reference it in the subclass. My constraint is self.ripHeight.constant Connected...

How to change or update NSLayoutConstraint programmatically

ios,objective-c,nslayoutconstraint
I have implemented AutoLayout programmatically using the Code : - (void)addConstraintWithListNavigationViewController:(UIView *)listViewNavigation y:(CGFloat)y height:(CGFloat)height { //WIDTH_ListTableView = 0.4 //set x = 0; NSLayoutConstraint *constraintToAnimate1 = [NSLayoutConstraint constraintWithItem:listViewNavigation attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:0.00 constant:0]; [self.view addConstraint:constraintToAnimate1]; //set y = y;...

Decrease amount of space between elements when space is restricted

ios,autolayout,nslayoutconstraint
Imagine the case where you have three buttons side by side. You want the three buttons to fill the available width, and be perfect squares with a minimum size set, with a set amount of space between them. To set this up, you would have the following constraints: A: 1:1...

Set constraints through code to an element from Storyboard with swift iOS8

ios,uitableview,swift,ios8,nslayoutconstraint
I have a ViewController with a tableView. I've set it up in the Storyboard. Is there a way to set the constraints for the tableView programmatically? I've tried to set a IBOutlet from my tableView in the ViewController and added constraints to it. But that didn't work. This is my...

Autolayout: origin and size should change according to width and height factor

ios,autolayout,nslayoutconstraint,iphone-6,adaptive-ui
Here is a scenario what I needed. I have put a UIButton on IB (size iPhone4 inch) whose initial frame on IB is x:100,y:100,w:100,h:100. I want the origins and height should change according to device size. for example the width and height of iPhone6 is 375X667, hence the width factor...

How do I retrive the value of a constraint?

ios,swift,nslayoutconstraint
I have a NSLayoutConstraint constraint: var myConstantLeading: CGFloat = 10 var myConstantTrailing: CGFloat = 10 var myConstraintLeading = NSLayoutConstraint (item: image, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: myConstantLeading) self.view.addConstraint(myConstraintLeading) var myConstraintTrailing = NSLayoutConstraint (item: image, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view,...

Adding an NSLayoutConstraint prgrammatically, crashes

ios,objective-c,nslayoutconstraint
I have an app in which I'm trying to apply NSLayoutConstraint to an item that is inside a view. Here is my code: [customCell.infoView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[customCell.infoLabel]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(customCell.infoView)]]; I don't know what this means, but it crashed with the following error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:...

xcode 6 - red constraints

ios,xcode,xcode6,interface-builder,nslayoutconstraint
I have been trying to tackle constraints recently. I have a mildly complex view. When connecting my constraints some of my constraints turn red. Simulator still runs, but my labels and buttons are not in place. Why are constraints red?

UILabel not wrapping reliably

ios,swift,uilabel,nslayoutconstraint
It seems impossible to get the UILabel to adhere to the constraints in the way that I want it to consistently. The leading and trailing space constraints seem in effect but the label is just truncating the lines instead of expanding to new lines. I do have the numberOfLines of...

AutoLayout complains about constraints for 2 UITextFields with no borders

ios,xcode,swift,autolayout,nslayoutconstraint
I am trying to build my login view that has only 2 TextFields, and 2 buttons. I am applying the "Add Missing Constraints" property so that Xcode applies the corresponding constraints to all the elements in my LoginViewController in the storyboard. I am restricting my app to run only in...

Slide in an UIButton and push other UIButton when a certain distance is reached

ios,objective-c,uiviewanimation,nslayoutconstraint
I would like to slide in a UIButton (grey color in screenshot) from the left to right and center it on the screen. There is an UIButton (orange color in screenshot) already though that is centered and this button should be pushed to the right and always have a distance...

Correct way to delete a NSLayoutConstraint

ios,xcode,swift,autolayout,nslayoutconstraint
In my apps I'm working a lot with constraints, in most cases with animations as well. In certain circumstances I need to remove constraints and add new ones. As I need to support iOS 7 as well, I'm not able to use the active property, which would else be the...

Views width fails to change properly with auto-layout

ios,objective-c,autolayout,nslayoutconstraint
I have 4 UIButtons in a static cell with width constraints and regular constraints on all 4 sides. At a certain point, I want to change the constraints width. Here is my code: - (void)viewDidLoad { [super viewDidLoad]; self.constraintArray = [[NSArray alloc] initWithObjects:self.budgetWidthConstraint, self.categorieWidthConstraint, self.extraButtonWidthConstraint, nil]; } - (void)myLayout {...

Can't change Constraint IBOutlet that is defined for different size classes in IB

ios,autolayout,interface-builder,nslayoutconstraint,size-classes
I made a special test app for this case. I added a view on my controller's view in Storyboard, set up AutoLayout constraints in Interface Builder and made one of them (vertical space) is defferent for different size classes. Screenshot from IB So the value is 100 for Any height,...

Add constraint before view is seen?

ios,swift,autolayout,uikit,nslayoutconstraint
I am trying to center an arrow button with a width = 40 in the middle of my screen using constraints programmatically. My code works if I call it in viewDidAppear but crashes in viewDidLoad. Having it in viewDidAppear is an eye sore since you see the screen and then...

Dynamically change auto constraints width

ios,objective-c,nslayoutconstraint
I have a custom cell with auto layout. I added a width constraint to a label. Is there a way to dynamically change its width? (For example when the user rotates the iphone)

Get error with NSLayoutConstraint when I set them to certain size

ios,objective-c,nslayoutconstraint
I have 4 UIButtons with constraints on all 4 sides of each, and a width constraint. (Xcode made me set the width constraint on 3 of them, but I did 4, just to make things simpler in the coding.) I then added all 4 width constraints to an NSArray. When...

Multiple Choice App Feature

ios,ios8,interface-builder,nslayoutconstraint
I am trying to create a simple multiple choice quiz feature for an app. I have a pList with all the questions, multiple choice options, and the answer. The questions have a different number of multiple choice options. Most questions have 4 options, but others have 5, 6, or 7....

Set constraint so that firstItem's top equals secondItem's height

ios,autolayout,nslayoutconstraint
I'd like to set a top constraint so that it has a relationship which is equal with another item's height. I prepare it in interface builder so that item 1's top is equal to item 2's top. Then, in code (because I don't think it can be done in Interface...

How do I retrive the value of the constant of a constraint in Swift?

ios,swift,nslayoutconstraint
I have a NSLayoutConstraint constraint that looks like this: var myConstant:CGFloat = 20 var myConstraint = NSLayoutConstraint (item: image, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: myConstant) self.view.addConstraint(myConstraint) When an UIButton is pressed, the image gets resized, therefore the constant of the image changes. self.image.transform =...

NSLayoutConstraint and assigning a variable that is an int

ios,swift,nslayoutconstraint
I feel this might be a very simple question as I'm just getting started with Swift but am a bit confused about this behavior. I have an NSLayoutConstraint that looks like this: let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0) and...

Positioning UILabel inside UIView

ios,autolayout,nslayoutconstraint
Given the following image: The three labels: "TODAY, AVERAGE, LAST WEEK" are positioned dynamically within my ViewController. This becomes a problem when the application is deployed to other devices (i.e. iPhone 6 Plus) where they aren't positioned properly. I'm aware of being able to use AutoLayout however I would like...

View with constraints changes when run on simulator

ios,storyboard,nslayoutconstraint
I have a UISegmentedControl, in a static UITableViewCell. I'm added constraints on all 4 sides. I want the segmentedControl to have squared corners. I set it up in the storyboard to look like that, and it looks fine. But when I run it on the simulator, you can see the...

Issue with constraints

ios,objective-c,autolayout,nslayoutconstraint
I have a UILabel and a UITextField that I created, and added constraint programmatically. Here is the code I inserted in viewDidLoad: UILabel *currencyLabel = [[UILabel alloc] init]; UITextField dollars = [[UITextField alloc] init]; CGRect label = CGRectMake(0, 0, 12, 33); currencyLabel.frame = label; CGRect textField = CGRectMake(12, 0, self.amountView.frame.size.width...

Prevent Interface Builder from auto creating Constraints?

ios,autolayout,interface-builder,nslayoutconstraint,xcode-storyboard
I created a demo project here. I have a view where I created a ScrollView in a .xib file. In interface builder I did not set any constraints. In my viewDidLoad method I set constraints with SnapKit: scrollView.snp_makeConstraints { (make) -> Void in make.edges.equalTo(self.view) } When I run the code...

View with auto layout doesn't resize

objective-c,autolayout,nslayoutconstraint
I have a static UITableViewCell in a containerView with auto layout applied. In the containerView is a UIView. I set the border color of the containerView to orange, and the view to green. When I run it on the simulator I get the following results: Constraints: Portrait: Landscape: The green...

animateWithDuration in UITableViewCell's setSelected method has no effect

ios,objective-c,uitableview,nslayoutconstraint,animatewithduration
I'm trying a simple animation for selection/deselection of a UITableViewCell like this: - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ tabConstraint.constant = selected ? 40 : 20; } completion:nil]; } The code inside the animations block will be called, but it's not animating. Everything works fine, but there's...

When I add autolayout, the views disapear

ios,objective-c,autolayout,nslayoutconstraint
I have 1 label and 1 UICollectionView in an overall view. I'm trying to get them like the image below: I can't use the storyboard to apply auto-layout, I have to do it programmatically, and I'm having difficulty doing that. Here is what I have: CGRect collectionViewFrame = CGRectMake(0, 20,...

Strange constraint behaviour in UIPageViewController

ios,objective-c,cocoa-touch,uipageviewcontroller,nslayoutconstraint
In storyboard I have UINavigationController without navigation bar and with root view controller UIPageViewController that has 2 children: LoginVC and ChoosePlaceVC. MainPageVC is a simple subclass of UIPageViewController where I just set [self setViewControllers:@[loginVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; In Login ViewController I set constraint to top for logo image to 0...

Swift constraints error (programmatic constrains)

ios,swift,xcode6,nslayoutconstraint
I've been trying to add constraints to 2 simple labels for several hours now. I can get one of them working without any problems, but the second I try to add constraints to my other label xcode provides the following error: Unable to simultaneously satisfy constraints. Probably at least one...

Programmatically retrieve the width and height of a UIView using Auto Layout and NSLayoutConstraints?

ios,swift,autolayout,nslayoutconstraint,visual-format-language
How do you get the width and height of a UIView who's size and position are set using Auto Layout and Apple's Visual Format Language? Here's the code (view is just the variable from UIViewController): // Create and add the view var stageView = UIView() stageView.setTranslatesAutoresizingMaskIntoConstraints(false) // Since I'm using...

How to prevent UILabel from being moved further than other objects

ios,swift,nslayoutconstraint
I am trying to move a label with an animation but I want it to be between two other objects. I thought I could do it with NSLayoutConstraints like let labelLeftConstraint = NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: self.leftObject, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0) let labelRightConstraint = NSLayoutConstraint(item:...

iOS 8 Autolayout - Visual Format Language how to add constraint to superview's edge

ios,swift,ios8,autolayout,nslayoutconstraint
Can I pin my view to my superView's edge instead of its layout margin through VFL? Can I be even flexible to toggle "Relative to margin" on myView as well? NO VFL (this is what I want to achieve with Visual Format Language): var constraintLeft = NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Left,...

Which UILabel method is invoked when I set text, and it resizes itself to fit?

ios,storyboard,uilabel,nslayoutconstraint
I have an UILabel, created in Universal storyboard, and I have mentioned all required constraints for its position OTHER THAN WIDTH. So it resizes itself as per the text set. Fantastic! Exactly what I want. Problem starts here : It has background color as green color, but that color is...

How can I enforce an UILabel to be wider than it should be, by 5 points?

ios,objective-c,uilabel,uistoryboard,nslayoutconstraint
I have an UILabel, it has background color as green. But It wraps the text very closely. I need the green background to be little wider than the text, approx 5 points. How can I achieve this? Details Label is created in storyboard and has horizontal spacing constraints. You can...

Trouble with UIScrollView and Constraints

uiscrollview,nslayoutconstraint
I'm having trouble with UIScrollView and Constraints. I want to display a FAQ inside my application. This will include several questions and answers, with various length. It won't change after launch, so I handle everything directly in the storyboard. However, it has to work on different iPhone screen size (from...

Why are NSLayoutConstraints ignored?

ios,nslayoutconstraint
I am experiencing a misunderstanding of mechanics of iOS Layout Constraints. See the code I placed inside viewDidLoad listed below. var btn = UIButton() btn.setTitle("i am a button", forState: UIControlState.Normal) btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) btn.backgroundColor = UIColor.lightGrayColor() btn.sizeToFit() view.addSubview(btn) view.addConstraint( NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute: NSLayoutAttribute.CenterX,...

Pointer to Visual Format Language (VFL) Constraint?

ios,objective-c,autolayout,nslayoutconstraint
I'm wondering if it is possible to obtain a pointer to a constraint created through the use of visual format language. For a simple example, assume I have created an array of constraints for a view such that it is pinned to the top, left and bottom of it's super...

How to handle NSLayoutConstraints with screen size/orientation changes in iOS 8/Swift?

ios,swift,nslayoutconstraint
How to work with IB Constraints and NSLayoutConstraint? I want to programmatically fine-tune my Interface Builder Storyboard layout, as I can't achieve desired layout with constraints in Interface Builder alone. The iPhone 4S and iPhone 6 Plus are particularly challenging for my iOS 8 layouts. Questions: How do I find...

Autolayout, size classes and font size

ios,swift,autolayout,nslayoutconstraint
I'm currently working with the following : Swift (1.2), Xcode (6.3) and iOS 8.3. I currently have an application that look similar to this sample I've made for this question : For each UIButton / UILabel I would like to configure their font size depending on which device is used...

Autolayout: Fallback constraints for neighbouring views when removing a view

ios,uiview,autolayout,nslayoutconstraint
I'm trying to make this work in a UICollectionReusableView (UICollectionView Header) context. The height required for strings in UILabels A, B, and C is calculated and set as the gray view's height somewhat like this: - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { .... CGFloat expectedHeightForLabelB = 0; if (stringB.length >...

Hiding a UIView using AutoLayout constraints

ios,uiview,autolayout,nslayoutconstraint
From time to time I have a subview that I would like to remove from a layout. Not only should it be hidden, but it should not be considered part of the view's 'flow', so to speak. An example: I am looking for a strategy to hide the orange view...

Constraints layout to UITextFields in a cell

storyboard,nslayoutconstraint
Trying to find options to add constraints to a UITextField that are in a table cell row. I'm trying to keep both of them 50% of the width and/either (pinned to the left right edges). Not sure what constraints to add to achieve it in the Interface builder. Please advise....

How to align Custom cells to Right Detail and Basic cells?

ios,iphone,xcode,layout,nslayoutconstraint
I am using static cells with 4 sections to create a Settings scene: For some cells I use the predefined styles: Right Detail and Basic. But for the following cells I use Custom style with my own constraints: Label on the left and Switch on the right side (like the...

Getting error when adding constraint

ios,objective-c,autolayout,uikit,nslayoutconstraint
I'm trying to set a constraint on 2 views so that they touch each other like this: I tried setting constraints programmatically: [self addConstraint:[NSLayoutConstraint constraintsWithVisualFormat:@"[_firstView][_secondView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_firstView, _secondView)]]; But I get the following warning: Incompatible pointer types sending 'NSArray *' to parameter of type 'NSLayoutConstraint' What am I doing...

NSLayoutConstraint constraintWithItem… ordering

ios,uikit,autolayout,nslayoutconstraint
Whenever I create an NSLayoutConstraint via +constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:, I always seem to end up putting the items in the wrong order, and then I have to reverse them. It is so annoying, and I can't seem to figure out any way to be able to tell before actually running the code...

UIButton size with Autolayout at runtime

ios,objective-c,uitableview,autolayout,nslayoutconstraint
I have found several questions on this but I haven't found a solid answer on how to really do this, so I hope this isn't a duplicate just for not finding a concrete answer. Here's my issue: I'm rewriting an old iOS app that has a tableview with custom cells,...

SegmentedControl changes position when run on simulator

ios,objective-c,uisegmentedcontrol,nslayoutconstraint
I have a UISegmentedControl, and I'm trying to set the widths of each segment. The code below shouldn't change anything, but it does. Here is the code: viewDidApear NSLog(@"%f", self.segment.frame.size.width); // 369 CGFloat segmentWidth = self.segment.frame.size.width; NSUInteger firstAndLastSegmentWidth = [self.segment widthForSegmentAtIndex:0] + [self.segment widthForSegmentAtIndex:4]; [self.segment setWidth:(segmentWidth - firstAndLastSegmentWidth) / 3...

What's wrong with my auto layout constraints?

ios,swift,autolayout,nslayoutconstraint
Here is my UIViewController before I press a button (I didn't put it there as it is not relevant): When I press the button, a UIView containing two UIPickerView appear from the top. Right under this UIView are 2 UIButtons. Everthing works with auto layout: Here is the code when...

Implementing an uiview subclass with children views using autolayout programmatically

ios,objective-c,uiview,autolayout,nslayoutconstraint
My need is very generic, I have to implement an UIView subclass which groups an UIImageView, x-centered on the screen and distant 20 pts from top, and a UILabel with a description. I have no problems doing it with interface builder, but since I needed to make it programmatically I...

iOS - changng constraint relation programatically

ios,xcode,interface-builder,nslayoutconstraint
given the following constraint in ios programtically: IBOutlet NSLayoutConstraint *myConstraint; this constraint is linked in interfacebuilder to the following details: How do I change the relation attribute programatically. I tried to look up for a method called setRelation but i dont see it. ...

When creating new tableView entry the constraints don't work

uitableview,ios8,nslayoutconstraint
I have a Master-Detail application that uses CoreData. Everything is hooked up fine and works as expected. There is just one problem with the display when adding a new entry. On the left is the Master's TableView with the entries and on the right is the Detail's View, where the...

Layout constraints for dynamically created buttons

ios,objective-c,autolayout,nslayoutconstraint
I am trying to create a view with dynamically created buttons. I am finding it difficult to set the constraints for inner objects other than first one that's created. Where is the issue? Create & Add buttons to view -(void) createButton:(NSString *) btnText isButton:(BOOL) type phraseWidth:(NSInteger) width view:(UIView *) currentView...

Getting correct frame after setting constraint

ios,objective-c,swift,autolayout,nslayoutconstraint
I have a method that creates a mask layer based on the current frame of my UIImageView, which works as I expected. However, in one case, I modify the height constraint of a UIImageView in the viewWillAppear method, and then apply the mask creator to the UIImageView, which creates a...

Setting a constraint in Auto Layout

ios,autolayout,nslayoutconstraint
I'm trying to set a constraint in Auto Layout. This: self.myHeightConstraint = 0.0f; gives me this error: Assigning to 'NSLayoutConstraint *' from incompatible type 'float' Setting it to 0 gives no errors but I'm not sure if that's doing what I want it to do. Basically I want to set...

Autolayout constraint with swift only applied after tap

ios,swift,autolayout,iad,nslayoutconstraint
I am showing an iAD banner like this: override func viewDidLoad() { ... initializeBannerAd() } func initializeBannerAd() { let banner = ADBannerView(adType: ADAdType.Banner) banner.frame = CGRectMake(0,-100, self.view.frame.width, 100) banner.delegate = self banner.hidden = true self.view.addSubview(banner) } func bannerViewDidLoadAd(banner: ADBannerView!) { banner.hidden = false banner.frame = CGRectMake(0, self.view.frame.size.height-100, self.view.frame.width, 100) banner.setTranslatesAutoresizingMaskIntoConstraints(false)...

textView height equal to content size without viewDidLayoutSubviews

ios,swift,nslayoutconstraint
I'm creating a viewController which contain 2 textViews a title and a fullText. At the moment i've created 2 textViews in the interface builder which is placed below each other and then created following code to change the height to equal to the content. However the issue is that it...