Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a button in middle of the UIView Horizontally and Vertically for any device?

I have a UIButton of type UIButtonRoundedRectType that I want to put in middle of the screen horizontally and vertically.

I've tried doing this by hardcoding numbers but this looks back when run on iPhone vs. iphone (retina). I would like to programmatically keep the button in the center by calculating the container's width.

So, I've tried this but it doesn't work.

container = view.frame.size
button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
button.frame = [[container.width/2-button.frame.size.width/2,150], [280,50]]
like image 350
Anthony Avatar asked Apr 11 '13 18:04

Anthony


2 Answers

Simple approach:

button.center = view.center;
like image 172
Darius Miliauskas Avatar answered Oct 21 '22 08:10

Darius Miliauskas


How about:

button.center = [button.superview convertPoint:button.superview.center fromView:button.superview.superview];
like image 36
lnafziger Avatar answered Oct 21 '22 09:10

lnafziger