Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically align a button in iOS Applications?

How will I be able to align a normal UIButton eg. on the top, bottom or middle programmatically?

like image 280
iHackerMe Avatar asked Sep 12 '12 12:09

iHackerMe


People also ask

How to create a button programmatically in iOS Swift 4?

How to create a button programmatically in iOS Swift 4 : Create a simple button :. Open XCode and create one SingleView iOS application. Give the project a name and select swift... Explanation :. First of all, define the x position,y position, height and width of the button we will create. Create ...

How to change the uilabel text alignment and color?

You can change label text, label text color, label background color, label border width, label border color by setting it’s properties. You can also change the label text alignment by change the UILabel object’s textAlignment property value t0 NSTextAlignment.left, NSTextAlignment.center or NSTextAlignment.right.

How do you add a button to a view?

First of all, define the x position,y position, height and width of the button we will create. Create one UIButton object and assign it to the button variable. Add frame to this variable by creating a CGRect using the values we have defined. Finally, add this button the view.


2 Answers

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

like image 158
Vimal Venugopalan Avatar answered Sep 19 '22 07:09

Vimal Venugopalan


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)];
like image 30
TheTiger Avatar answered Sep 19 '22 07:09

TheTiger