Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewAutoresizingMask for pinning a button to bottom right corner

I want to pin a UIButton to bottom right corner of the iPhone screen. Both in 3.5inch and 4inch screens.

Here is the code I wrote. But the button always stays on x=265,y=361 both in iPhone4 and iPhone5.

UIImage* plusImage = [UIImage imageNamed:@"Plus.png"];
UIButton* plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[plusButton setImage:plusImage forState:UIControlStateNormal];
[plusButton setFrame:CGRectMake(265, 361, plusImage.size.width, plusImage.size.height)];
plusButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:plusButton];

I've set the autoresizing masks to take flexible top and left margins.But it didnt work. What did I miss? Please help.

like image 211
Anand Avatar asked Dec 19 '25 23:12

Anand


1 Answers

UIImage* plusImage = [UIImage imageNamed:@"Plus.png"];
UIButton* plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[plusButton setImage:plusImage forState:UIControlStateNormal];
CGSize viewSize = self.view.frame.size;
[plusButton setFrame:CGRectMake(viewSize.width - plusImageSize.width, viewSize.height - plusImageSize.height, plusImage.size.width, plusImage.size.height)];
plusButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:plusButton];

Try that instead.

like image 189
Guy Kogus Avatar answered Dec 22 '25 14:12

Guy Kogus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!