I have a simple setup with 3 views:
view, which has 1 subview contentView, which has 1 subview emailField (a UITextField).
emailfField is not aligning in centerY correctly. I got is this error:
"<NSLayoutConstraint:0x9e7d500 UIView:0x9e85c30.centerY == UITextField:0x9e4dbd0.centerY>",
"<NSLayoutConstraint:0x9e7cc00 UIView:0x9e85530.top == UIView:0x9e85c30.top>",
"<NSAutoresizingMaskLayoutConstraint:0x9e7f950 h=--& v=--& UITextField:0x9e4dbd0.midY == + 282.5>",
"<NSAutoresizingMaskLayoutConstraint:0x9e81e90 h=--& v=--& UIView:0x9e85c30.midY == + 240>")
My .m file:
- (void)viewDidLoad {
[super viewDidLoad];
[self initContentView];
[self initFirstView];
[self addConstraints];}
- (void)addConstraints {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
//First View
//Email Field
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.emailField
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];}
- (void)initContentView {
self.view.backgroundColor = [UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0];
self.contentView = [[UIView alloc] initWithFrame:self.view.frame];
self.contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.contentView];
int frameHorizontalCenter = self.contentView.frame.size.width/2;
self.funView = [[UIImageView alloc] initWithFrame:CGRectMake(frameHorizontalCenter-50, 100, 100, 100)];
self.funView.backgroundColor = [UIColor clearColor];
self.funView.image = [UIImage imageNamed:#"dog1"];
[self.contentView addSubview:self.funView];
self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameHorizontalCenter - 140, self.funView.frame.origin.y + self.funView.frame.size.height, 280, 60)];
self.descriptionLabel.font = [UIFont systemFontOfSize:14];
self.descriptionLabel.textColor = [UIColor colorWithRed:63.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0];
self.descriptionLabel.backgroundColor = [UIColor clearColor];
self.descriptionLabel.highlightedTextColor = [UIColor whiteColor];
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.descriptionLabel.numberOfLines = 2;
self.descriptionLabel.adjustsFontSizeToFitWidth = YES;
self.descriptionLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.descriptionLabel];
self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(frameHorizontalCenter -140, self.descriptionLabel.frame.origin.y + self.descriptionLabel.frame.size.height, 280, 45)];
self.emailField.borderStyle = UITextBorderStyleNone;
self.emailField.textColor = [UIColor colorWithRed:63.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0];
self.emailField.backgroundColor = [UIColor whiteColor];
self.emailField.placeholder = #"Epasts";
[self.contentView addSubview:self.emailField];}
I really don't understand what's wrong. Aren't constraints set correctly?
Use setTranslatesAutoresizingMaskIntoConstraints: to turn off conversion of autoresizing masks to constraints on contentView and emailField. Add additional constraints to emailField to specify the x position, width and height that it should use.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on application which will work only horizontally and doing everything programmatically.
I am having a imageview, two textfield and two buttons but I don't know how to set constraints on these textfields and buttons, means when I am using in iPhone 5s then it is fine but when I am using iPhone 6s plus then it looks small.
I am posting here some code and screenshots
UIImageView *logoImage = [[UIImageView alloc]init];
logoImage.frame = CGRectMake(CGRectGetMidX(self.view.frame)-100, 10, 250, 100);
[logoImage setImage:[UIImage imageNamed:#"antya_logo1.png"]];
[self.view addSubview:logoImage];
UILabel *loginLabel = [[UILabel alloc]init];
loginLabel.frame = CGRectMake(10, 0, 100, 40);
loginLabel.text = #"Login Form";
loginLabel.textColor = [UIColor blackColor];
loginLabel.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
CGFloat Xuser = CGRectGetMidX(self.view.frame)-120;
CGFloat Yuser = CGRectGetMaxY(loginLabel.frame)+80;
usernameField = [[UITextField alloc]init];
usernameField.frame = CGRectMake(Xuser, Yuser, 300, 35);
usernameField.placeholder = #" User Name";
usernameField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.layer.cornerRadius = 7;
usernameField.layer.borderWidth = 0.5;
[self.view addSubview:usernameField];
UIImageView *userImgV = [[UIImageView alloc]init];
userImgV.frame = CGRectMake(CGRectGetMinX(usernameField.frame)-35, CGRectGetMinY(usernameField.frame)+5, 25, 25);
[userImgV setImage:[UIImage imageNamed:#"user-icon.png"]];
[self.view addSubview:userImgV];
CGFloat Ypass = CGRectGetMaxY(usernameField.frame)+20;
passwordField = [[UITextField alloc]init];
passwordField.frame = CGRectMake(Xuser, Ypass, 300, 35);
passwordField.placeholder = #" Password";
passwordField.secureTextEntry = YES;
passwordField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.layer.cornerRadius = 7;
passwordField.layer.borderWidth = 0.5;
[self.view addSubview:passwordField];
this is in i phone 6s plus
and this is in i phone5s
Please help me,thanks in advance
Note : When you are dealing with autolayout setting frame of a view is not going to work. You need to set constraints for that view to look correctly in all iPhone devices.
UIImageView *logoImage = [[UIImageView alloc] init];
[logoImage setImage:[UIImage imageNamed:#"antya_logo1.png"]];
logoImage.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:logoImage];
UILabel *loginLabel = [[UILabel alloc]init];
loginLabel.text = #"Login Form";
loginLabel.textColor = [UIColor blackColor];
loginLabel.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
loginLabel.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:loginLabel];
UITextField *usernameField = [[UITextField alloc]init];
usernameField.placeholder = #" User Name";
usernameField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
usernameField.backgroundColor = [UIColor whiteColor];
usernameField.layer.cornerRadius = 7;
usernameField.layer.borderWidth = 0.5;
usernameField.translatesAutoresizingMaskIntoConstraints = false;
UIImageView *userImgV = [[UIImageView alloc]init];
[userImgV setImage:[UIImage imageNamed:#"user-icon.png"]];
//set left view of textfield
usernameField.leftView = userImgV;
usernameField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:usernameField];
UITextField *passwordField = [[UITextField alloc]init];
passwordField.placeholder = #" Password";
passwordField.secureTextEntry = YES;
passwordField.font = [UIFont fontWithName:#"LaoSangamMN" size:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.layer.cornerRadius = 7;
passwordField.layer.borderWidth = 0.5;
passwordField.translatesAutoresizingMaskIntoConstraints = false;
//set left view of textfield
UIImageView *passwordImgV = [[UIImageView alloc]init];
[passwordImgV setImage:[UIImage imageNamed:#"password-icon.png"]];
passwordField.leftView = passwordImgV;
passwordField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:passwordField];
UIButton *buttonSignUp = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonSignUp setTag:101];
[buttonSignUp setTitle:#"SIGNUP" forState:UIControlStateNormal];
[buttonSignUp addTarget:self action:#selector(<your selector>) forControlEvents:UIControlEventTouchUpInside];
buttonSignUp.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:buttonSignUp];
UIButton *buttonFP = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonFP setTag:101];
[buttonFP setTitle:#"SIGNUP" forState:UIControlStateNormal];
[buttonFP addTarget:self action:#selector(<your selector>) forControlEvents:UIControlEventTouchUpInside];
buttonFP.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:buttonFP];
//setting constraints
//logoImage
//leading
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//Top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//traling
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:logoImage attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
// usernameField
//leading
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:usernameField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//traling
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:usernameField attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
//top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoImage attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:usernameField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//passwordField
//leading
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:passwordField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//traling
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
//top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:passwordField attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//buttonSignUp and buttonFP
//leading for buttonSignUp
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonSignUp attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10]];
//traling for buttonFP
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:buttonFP attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10]];
//equal width for both
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
//space between both btns
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeRight multiplier:1.0 constant:10]];
//top for both
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonSignUp attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonFP attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:passwordField attribute:NSLayoutAttributeTop multiplier:1.0 constant:10]];
//bottom
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:buttonFP attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:buttonSignUp attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];
After adding #IBOutlet each of items on ViewController. You can use Auto Layout Visual Format Language to implement programmatically constraints. Here's sample code to understand from my project.
(Swift Version) If cell awakeFromNib or or page you can use in viewDidLoad:
avaImg.translatesAutoresizingMaskIntoConstraints = false
usernameBtn.translatesAutoresizingMaskIntoConstraints = false
infoLbl.translatesAutoresizingMaskIntoConstraints = false
dateLbl.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-10-[ava(30)]-10-[username]-7-[info]-10-[date]",
options: [], metrics: nil, views: ["ava":avaImg, "username":usernameBtn, "info":infoLbl, "date":dateLbl]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[ava(30)]-10-|",
options: [], metrics: nil, views: ["ava":avaImg]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[username(30)]",
options: [], metrics: nil, views: ["username":usernameBtn]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[info(30)]"
, options: [], metrics: nil, views: ["info":infoLbl]))
self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-10-[date(30)]",
options: [], metrics: nil, views: ["date":dateLbl]))
Additional link cited before Using Autolayout Visual Format with Swift?
(Objective-C Version) You can find here many details to do same.
Something strange is happening when I have UITextfield, UILabel and UIButton. I am applying constraints so that the label and the textfield are centred in the UIView. The button is pinned at the bottom. When I run this code, UITextField becomes non-editable. Can somebody tell me what is going wrong with this code below?
Click here to view the code output
UIView* topLayout = [[UIView alloc]init];
[topLayout setTranslatesAutoresizingMaskIntoConstraints:NO];
topLayout.backgroundColor = [UIColor whiteColor];
[self.view addSubview: topLayout];
NSArray *topLayoutHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:[parentView(==mainView)]" options:0 metrics:0 views:#{#"parentView": topLayout, #"mainView":self.view}];
NSArray *topLayoutVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[parentView]|" options:0 metrics:0 views:#{#"parentView": topLayout}];
[self.view addConstraints:topLayoutHConstraints];
[self.view addConstraints:topLayoutVConstraints];
UIView *childViewLayout = [[UIView alloc]init];
childViewLayout.backgroundColor = [UIColor yellowColor];
[childViewLayout setTranslatesAutoresizingMaskIntoConstraints:NO];
[topLayout addSubview:childViewLayout];
NSLayoutConstraint *childViewControlsHConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *childViewControlsVConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *childViewControlsHtConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[topLayout addConstraint:childViewControlsHConstraints];
[topLayout addConstraint:childViewControlsVConstraints];
//[topLayout addConstraint:childViewControlsHtConstraints];
// Instruction field
UILabel* instructionFieldLbl = [[UILabel alloc]init];
[childViewLayout addSubview:instructionFieldLbl];
[instructionFieldLbl setText:#"Please check your SMS or E-Mail for the 4 digit actiation code. Please enter the same to activat your account"];
UIFont *instructionFieldLblFont = [UIFont fontWithName:#"Arial-BoldMT" size:13];
[instructionFieldLbl setFont:instructionFieldLblFont];
instructionFieldLbl.numberOfLines = 0;
[instructionFieldLbl sizeToFit];
[instructionFieldLbl setTextColor:[UIColor grayColor]];
instructionFieldLbl.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *instructionFieldHConstraint = [NSLayoutConstraint constraintWithItem:instructionFieldLbl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationLessThanOrEqual toItem:childViewLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0f];
//NSArray *instructionFieldHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[instructionFieldLbl]-10-|" options:0 metrics:nil views:#{#"instructionFieldLbl": instructionFieldLbl}];
[childViewLayout addConstraint:instructionFieldHConstraint];
//[childViewLayout addConstraints:instructionFieldHConstraints];
dispatch_async(dispatch_get_main_queue(), ^{
instructionFieldLbl.preferredMaxLayoutWidth = self.view.bounds.size.width;
});
// Activation field
self->m_ObjActivationField = [[JVFloatLabeledTextField alloc]init];
[childViewLayout addSubview:self->m_ObjActivationField];
self->m_ObjActivationField.borderStyle = UITextBorderStyleRoundedRect;
self->m_ObjActivationField.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjActivationField.placeholder = [NSString stringWithFormat:#"Enter the activtion code"];
NSLayoutConstraint *activationHorzConstraint = [NSLayoutConstraint constraintWithItem:self->m_ObjActivationField attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:childViewLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[childViewLayout addConstraint:activationHorzConstraint];
NSDictionary *myControlViews = #{
#"instructionFieldLbl": instructionFieldLbl,
#"activationField": self->m_ObjActivationField
};
NSArray *myControlViewsVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[instructionFieldLbl]-10-[activationField]|" options:0 metrics:nil views:myControlViews];
[childViewLayout addConstraints:myControlViewsVConstraints];
// Submit button
self->m_ObjActivationBut = [[UIButton alloc]init];
[topLayout addSubview:self->m_ObjActivationBut];
[self->m_ObjActivationBut setTitle: [NSString stringWithFormat:#"Activate"] forState:UIControlStateNormal];
self->m_ObjActivationBut.backgroundColor = [UIColor redColor];
[self->m_ObjActivationBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self->m_ObjActivationBut.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjActivationBut.layer.cornerRadius = 10;
self->m_ObjActivationBut.clipsToBounds = YES;
NSDictionary *topViews = #{
#"childViewLayout": childViewLayout,
#"activationButton": self->m_ObjActivationBut
};
//NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[activationButton(40)]-10-|" options:0 metrics:nil views:topViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[activationButton]-|" options:0 metrics:nil views:#{#"activationButton": self->m_ObjActivationBut}];
NSLayoutConstraint *myTopVConstraints = [NSLayoutConstraint constraintWithItem:self->m_ObjActivationBut attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0f];
[topLayout addConstraint:myTopVConstraints];
[topLayout addConstraints:myTopHConstraints];
i add subView(backgroundView) in uiview in my base view controller its working well in potratate but when i change orientation from potraite to landscape its frame size is same as potrait i want to change size of subview when rotate.
UIView *activityView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect frame = activityView.frame;
activityView.frame = frame;
activityView.backgroundColor = [UIColor clearColor];
activityView.alpha = 0.0f;
[self.view addSubview:activityView];
self.activityView = activityView;
UIView *backgroundView = [[UIView alloc]initWithFrame:activityView.bounds];
backgroundView.alpha = 0.0f;
[backgroundView setBackgroundColor:[UIColor lightGrayColor]];
[self.activityView backgroundView];
UIActivityIndicatorView *spinning = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.activityView spinning];
spinning.center = activityView.center;
self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
[spinning setColor:[UIColor lightGrayColor]];
[spinning startAnimating];
To do this you must add constraints to your view (AutoLayout).
For example :
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
Edit :
Don't forget to set translatesAutoresizingMaskIntoConstraints property to NO on views.
(Replace self.view and containerView with your views).
This is my updated question !
I have searched many tutorial & sites for setting Auto layout constraints to set my UIButton at bottom-centre of my view controller.I use code to create uiButton and i have set position,but typically i can see my uiButton are positioning in different place in different simulator(4S,5,6,INFACT ON MY OWN DEVICE).i need to set my uibutton at bottom-centre Like this image
I am new to ios ,so can't able to set constraint for my UIButton.And this is my UIButton code:
self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self->closeBtn.frame = CGRectMake(260, 30, 50, 28);
self->closeBtn.layer.cornerRadius = 4;
self->closeBtn.layer.borderWidth = 1;
self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
[self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
[self->closeBtn setTitle:#"Done" forState:UIControlStateNormal];
[self->closeBtn.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0]];
[self.view addSubview:self->closeBtn];
[self->closeBtn addTarget:self action:#selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint * c_1 =[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self->closeBtn
attribute:NSLayoutAttributeRight
multiplier:1.0 constant:60];
NSLayoutConstraint * c_2 =[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self->closeBtn
attribute:NSLayoutAttributeTop
multiplier:1.0 constant:-1*60];
NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:70];
NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:28];
[self.view addConstraints:#[c_1,c_2]];
[self->closeBtn addConstraints:#[equal_w,equal_h]];
This above code is set for at top-right.so i have changed that to bottom,centre but i can't able to see my button.i need my button Like this image button position Not able to set constraint to place my uibutton position at in same place.Kindly any one can help me out to solve my problem
I have check your code.Replace this below code with your code .You can get your button at position as you required
UIView *superview = self.view;
// Do any additional setup after loading the view, typically from a nib.
self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self->closeBtn.frame = CGRectMake(260, 100, 50, 28);
self->closeBtn.layer.cornerRadius = 4;
self->closeBtn.layer.borderWidth = 1;
self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
[self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
[self->closeBtn setTitle:#"Done" forState:UIControlStateNormal];
[self->closeBtn.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0]];
[self.view addSubview:self->closeBtn];
[self->closeBtn addTarget:self action:#selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint * c_1 =[NSLayoutConstraint
constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
NSLayoutAttributeCenterX multiplier:1.0 constant:-7.5f];
NSLayoutConstraint * c_2 =[NSLayoutConstraint
constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual toItem:superview attribute:
NSLayoutAttributeCenterY multiplier:1.85f constant:0.0f];
NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:50];
NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:28];
[self.view addConstraints:#[c_1,c_2]];
[self->closeBtn addConstraints:#[equal_w,equal_h]];
Hope this helpful.Please learn some tutorial for auto layout.sure this concepts are very helpful for making App ...
I have created a custom view to show 2 labels inside a circle. It is suppose to look like this:
but in iOS 7 it looks like this:
That happens because the constraints hat are supposed to stick the labels to the top/bottom are breaking for some reason, but only on iOS 7. I get the following error:
(
"<NSLayoutConstraint:0x7f96e500a540 H:|-(-1)-[UILabel:0x7f96e2dd84d0] (Names: '|':UIView:0x7f96e2df6350 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f96e500a540 H:|-(-1)-[UILabel:0x7f96e2dd84d0] (Names: '|':UIView:0x7f96e2df6350 )>
I have confirmed that those are in fact the constraints called (I only gave one error as an example)
self.dayLabelStickButtomConstraint
self.monthLabelStickTopConstraint
Here is the code for the custom view, can anyone maybe understand why is it breaking on iOS 7 but not on 8?
- (instancetype) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initiateValues];
[self buildHeirarchy];
}
return self;
}
- (instancetype) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initiateValues];
[self buildHeirarchy];
}
return self;
}
-(void)initiateValues{
self.fillColor = [UIColor blackColor];
self.month = #"jan";
self.monthFontSize = 8.0f;
self.monthTextColor = [UIColor blackColor];
self.monthLabelInsetDistance = 1.0f;
self.day = #"01";
self.dayFontSize = 12.0f;
self.dayTextColor = [UIColor blackColor];
self.dayLabelInsetDistance = 1.0f;
}
-(void)buildHeirarchy{
self.innerContainer = [[UIView alloc] initWithFrame:CGRectInset(self.bounds, 5.0, 5.0)];
[self.innerContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:self.innerContainer];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.innerContainer
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.innerContainer
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.innerContainer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.7
constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.innerContainer
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.7
constant:0.0]];
self.monthLabel = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 5.0, 5.0)];
[self.monthLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
self.monthLabel.textAlignment = NSTextAlignmentCenter;
[self.innerContainer addSubview:self.monthLabel];
[self.innerContainer addConstraint:[NSLayoutConstraint constraintWithItem:self.monthLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.innerContainer
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
NSLayoutConstraint *monthTopConstraint = [NSLayoutConstraint constraintWithItem:self.monthLabel
attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationEqual
toItem:self.innerContainer
attribute:NSLayoutAttributeTopMargin
multiplier:1.0
constant:self.monthLabelInsetDistance];
self.monthLabelStickTopConstraint = monthTopConstraint;
[self.innerContainer addConstraint:self.monthLabelStickTopConstraint];
self.dayLabel = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 5.0, 5.0)];
[self.dayLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.innerContainer addSubview:self.dayLabel];
self.dayLabel.textAlignment = NSTextAlignmentCenter;
[self.innerContainer addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.innerContainer
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
NSLayoutConstraint *dayButtomConstraint = [NSLayoutConstraint constraintWithItem:self.dayLabel
attribute:NSLayoutAttributeBottomMargin
relatedBy:NSLayoutRelationEqual
toItem:self.innerContainer
attribute:NSLayoutAttributeBottomMargin
multiplier:1.0
constant:self.dayLabelInsetDistance];
self.dayLabelStickButtomConstraint = dayButtomConstraint;
[self.innerContainer addConstraint:self.dayLabelStickButtomConstraint];
}
-(void)layoutSubviews{
// get the graphics context
if (!self.backgroundColorRoundView) {
self.backgroundColorRoundView = [[UIView alloc]initWithFrame:self.bounds];
[self addSubview:self.backgroundColorRoundView];
[self sendSubviewToBack:self.backgroundColorRoundView];
self.backgroundColorRoundLayer = [CAShapeLayer new];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.backgroundColorRoundView.bounds];
self.backgroundColorRoundLayer.path = path.CGPath;
//imageMaskLayer.fillColor = self.fillColor.CGColor;
[self.backgroundColorRoundView.layer addSublayer:self.backgroundColorRoundLayer];
}
self.backgroundColorRoundView.frame = self.bounds;
self.backgroundColorRoundLayer.frame = self.bounds;
[self updateViews];
[super layoutSubviews];
}
-(void)updateViews{
self.monthLabel.textColor = self.monthTextColor;
self.dayLabel.textColor = self.dayTextColor;
self.backgroundColorRoundLayer.fillColor = self.fillColor.CGColor;
}
-(void)updateConstraints{
self.dayLabelStickButtomConstraint.constant = self.dayLabelInsetDistance;
self.monthLabelStickTopConstraint.constant = self.monthLabelInsetDistance;
[super updateConstraints];
}
After a lot of messing around the answer lied in the restraints themselves. Once i switched from topMargin,bottomMargin to just top and bottom it worked