1
votes

I am learning auto layout by code. For my learning purpose i am doing sample apps using autolayout. In my sample app i decided to keep text field below 20 points from top margin. Here is the code

#import "SampleViewController.h"
@interface SampleViewController (){
UITextField *username;
}
@end
@implementation SampleViewController

- (void)viewDidLoad {
[super viewDidLoad];
 self.edgesForExtendedLayout = UIRectEdgeNone;
[self prepareViews];
}

-(void)prepareViews{
   username = [[UITextField alloc]init];
   username.placeholder = @"Username";
   [username setBorderStyle:UITextBorderStyleRoundedRect];
   username.translatesAutoresizingMaskIntoConstraints = NO;

   [self.view addSubview:username];
   [self prepareConstraint];
}
-(void)prepareConstraint{
   NSLayoutConstraint *constraint = [NSLayoutConstraint
                      constraintWithItem:username
                      attribute:NSLayoutAttributeCenterX
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeCenterX
                      multiplier:1.0
                      constant:0];
    [self.view addConstraint:constraint];

         constraint = [NSLayoutConstraint
                      constraintWithItem:username
                      attribute:NSLayoutAttributeWidth
                      relatedBy:NSLayoutRelationEqual
                      toItem:nil
                      attribute:NSLayoutAttributeNotAnAttribute
                      multiplier:1.0
                      constant:250];
    [self.view addConstraint:constraint];

         constraint = [NSLayoutConstraint
                      constraintWithItem:username
                      attribute:NSLayoutAttributeTopMargin
                      relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                      attribute:NSLayoutAttributeTopMargin
                      multiplier:1.0
                      constant:20];
    [self.view addConstraint:constraint];
   }
   @end

When I run this code on iphone 4(iOS 7.1) i got an output like this iPhone 4 (iOS 7.1) Then when I run this code on iphone 5S(iOS 8.4) i got an output like this iPhone 5s (iOS 8.4)

Guys i don't know where i am making mistake.Kindly point out me where i am making mistake.And i got this warning when i run this code on iphone 4 (iOS 7.1) .

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) ( "<NSLayoutConstraint:0x14dcac70 H:|-(20)-[UITextField:0x14d955b0] (Names: '|':UIView:0x14d9b480 )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x14dcac70 H:|-(20)-[UITextField:0x14d955b0]
(Names: '|':UIView:0x14d9b480 )>

Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

1
change toitem in 3rd contraint from self.view to self.topLayoutGuide and make toitem attributr to bottom - EI Captain v2.0
@Bhavin I make the changes what you have mentioned ,After i run the code in iPhone 4 app crashes but it works fine in 5s. - Balaji
what is the log of that crash? - EI Captain v2.0
@Bhavin This is the crash report. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil' - Balaji
@Bhavin Same issue problem not solved . In your iphone 4s which iOS version you are using. - Balaji

1 Answers

3
votes

This is tested in 4s and 5s....you just add width constraint to view rather then you should apply it to textfield...

NSLayoutConstraint *constraint = [NSLayoutConstraint
                                  constraintWithItem:username
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                                  attribute:NSLayoutAttributeCenterX
                                  multiplier:1.0
                                  constant:0];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint
              constraintWithItem:username
              attribute:NSLayoutAttributeWidth
              relatedBy:NSLayoutRelationEqual
              toItem:nil
              attribute:NSLayoutAttributeNotAnAttribute
              multiplier:1.0
              constant:250];
[username addConstraint:constraint];

constraint = [NSLayoutConstraint
              constraintWithItem:username
              attribute:NSLayoutAttributeTopMargin
              relatedBy:NSLayoutRelationEqual
              toItem:self.topLayoutGuide
              attribute:NSLayoutAttributeBottom
              multiplier:1.0
              constant:20];
[self.view addConstraint:constraint];