How will I be able to align a normal UIButton eg. on the top, bottom or middle programmatically?
You have to set UIButton
frame your self.
Horizontally Alignment
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
[button setFrame:CGRectMake(X_Co, yourYposition, yourButtonWidth, yourButtonheight)];
Vertically Align
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(yourXposition, Y_Co, yourButtonWidth, yourButtonheight)];
TOP LEFT
[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)];
TOP RIGHT
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
BOTTOM LEFT
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
BOTTOM RIGHT
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
Center Of self.view
button.center = self.view.center;
OR
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
You can set the center of button to place it accordingly
yourButton.center = CGPointMake(0.0, 0.0);// for topleft
yourButton.center = CGPointMake(160.0, 240.0);// for center
yourButton.center = CGPointMake(320.0, 480.0);// for bottomright
Hope this helps
The only way to set the position of a subview is manually. You can set the frame, like this, for example:
Or change the origin, like this, for example:
view.frame.origin.x = INT_VALUE;
The only way to get some kind of alignment is to do math with self.view.frame
. If you want to align an object horizontally with the middle of the screen, use this:
view.frame.origin.x = self.view.frame.size.width / 2
If you want to align something to the top with a margin of 44 pixels, use this:
view.frame.origin.y = self.view.frame.origin.y = (view.frame.size.height / 2) + 44;
Etc.
You should set its frame.
Something like
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)];
or
button.frame = CGRectMake(320, 120, 35, 10);
Keep in mind that the first couple of values are respectively the width and the height (position in X and Y) of the view and the second couple of values are the width and the height of the button itself.
So, if you want a button on top, you should type
button.frame = CGRectMake(0, 0, 35, 10);
Middle
button.frame = CGRectMake(160, 240, 35, 10);
Bottom
button.frame = CGRectMake(320, 480, 35, 10);
great tip of the @TheTiger! I put this logic in a swift class:
https://github.com/iurims/iSLibs/blob/master/ISPositionCalculator.swift
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.