How will I be able to align a normal UIButton eg. on the top, bottom or middle programmatically?
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 ...
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.
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.
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
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)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With