1

I need help. I'm designing my UI, and I have been using the properties FRAME and CENTER to layout my views. Both of those are so much helpful to me. I also use the technique of adding my elements as subview of UIViews container.

But, right now, I want to place a UIView at the top of another. Please take a look at my picture. The buttons at the bottom of the screen is already set and is perfect right now, I'm only having a hard time to place my UIView (a container of my textfields) above the button/divider.

enter image description here

I'm only starting in iOS, I think 1 month plus already. I know basics of constraints programmatically but I don't want to use this for now as much as possible.

PS. I make my screen programmatically.

Sample of my code in this screen

// start adding container UIView *container2 = [[UIScrollView alloc] init]; container2.frame = CGRectMake(0, 0, [TPRConstants screenWidth] * 0.95, heightOfScrollView); container2.backgroundColor = [UIColor blueColor]; container2.contentSize = CGSizeMake([TPRConstants screenWidth] * 0.95, 250); [self.view container2]; // stuffs and subviews/textfields inside the container self.phoneTextField = [[UITextField alloc] init]; _phoneTextField.frame = CGRectMake(0, 200, scrollView.frame.size.width, 50); _phoneTextField.delegate = self; _phoneTextField.borderStyle = UITextBorderStyleNone; _phoneTextField.background = [UIImage imageNamed:@"textfieldLogIn.png"]; _phoneTextField.backgroundColor = [UIColor clearColor]; [_phoneTextField setKeyboardType:UIKeyboardTypeNumberPad]; [container2 addSubview:_phoneTextField]; // add container for divider and log in button UIView *container = [[UIView alloc] init]; container.frame = CGRectMake(0, 0, [TPRConstants screenWidth], 80); container.backgroundColor = [UIColor whiteColor]; container.center = CGPointMake([TPRConstants screenWidth] * 0.50, [TPRConstants screenHeight] - container.frame.size.height/2); [self.view addSubview:container]; // add divider UIImageView *divider = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, container.frame.size.width, 5)]; divider.image = [UIImage imageNamed:@"dividerCP.png"]; divider.backgroundColor = [UIColor clearColor]; divider.contentMode = UIViewContentModeCenter; [container addSubview: divider]; // add save login UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; saveBtn.frame = CGRectMake(0, 0, container.frame.size.width * 0.95, 50); saveBtn.center = CGPointMake(container.frame.size.width /2 , (container.frame.size.height - saveBtn.frame.size.height/2) - 10 ); [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [saveBtn setBackgroundImage:[UIImage imageNamed:@"logoutupCP.png"] forState:UIControlStateNormal]; [saveBtn setTitle:@"Save" forState:UIControlStateNormal]; [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; [saveBtn setBackgroundImage:[UIImage imageNamed:@"logoutdownCP.png"] forState:UIControlStateHighlighted]; [container addSubview: saveBtn]; 
1
  • 1
    Welcome to stackoverflow! What exactly is your question? Commented Oct 27, 2015 at 14:29

2 Answers 2

1

In iOS everything in a view is added as a stack i.e on top of each other. So your button will always be on top of the divider as it is added last.

If you try your solution now on several screens, you will find that all of the items will be in different positions.

So you have to add constraints.

The easiest way is via storyboard.

Keep your textfields in one view and your button and divider in another view and add a vertical spacing constraint between both views.

Note: Ofcourse you have to add all the required trailing, leading, top and bottom constraints on each textField and the container views and separator image in view plus center horizontally and vertically constraints to the button.

Sign up to request clarification or add additional context in comments.

Comments

0

If you wish to place a UIView at the base of phoneTextField use the maximum y position of phoneTextField for the y origin position of the UIView.

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(phoneTextField.frame)+0,[TPRConstants screenWidth],50); newView.backgroundColor = [UIColor blueColor]; [self.view addSubview:newView]; 

Then change the saveBtn frame to make space for newView:

saveBtn.frame = CGRectMake(0, CGRectGetMaxY(newView.frame), container.frame.size.width * 0.95, 50); 

2 Comments

thanks for the answer, I get your idea. However, the container of my SAVE button is my reference for the container of the textfields. I solved my problem by adding all those elements into 1 container. Thanks again!
Ah I see, I usually work top down adding views rather than bottom up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.