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
Then when I run this code on iphone 5S(iOS 8.4) i got an output like this

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.
self.viewtoself.topLayoutGuideand make toitem attributr to bottom - EI Captain v2.0