-1

I would like to know if there is any possible way through which I can dynamically change the position of a UIButton depending on screen size of the phone(4s, 5-series and 6)

Thanks in advance.

sam24
  • 99
  • 2
  • 10

2 Answers2

0

There are multiple ways to solve this issue:

1) With AutoLayout you can specify rules which describe how views relate to each other. This will allow the app to intelligently adapt to any screen size, if thoughtfully implemented.

2) You can check the device version at runtime and programmatically layout your view appropriately.

1 is the preferred solution, and is best practice on iOS.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • Well, the button I'm talking about is added programmatically and since I'm new to auto Layouting, I have no idea as to how to do it programmatically. Could you please help me with it? – sam24 Jun 19 '15 at 12:12
  • 1
    In that case you need to add the auto layout rules programmatically. For me to help you I would need to see your view as it's currently laid out and a description of what you want it to look like. On a side note, I recommend you 'accept' the answer people give to your questions. If you never take the time to accept answer that people give, people are less likely to want to help you. – Woodstock Jun 19 '15 at 12:16
0

As Woodstock as suggested you can use Autolayout. But if you are familiar with it and have a simple use-case, you can use the [UIScreen mainScreen] bounds].size.height property to get height of the screen and compare it with the sizes of the different devices using the if/switch and then accordingly call the following in each of the cases to adjust its position accordingly.

[button setCenter:<CGPoint calculated goes here>];

For the Autolayout approach you can checkout this answer.

Community
  • 1
  • 1
Vijay Tholpadi
  • 2,135
  • 1
  • 15
  • 20
  • This method should not be recommended. AutoLayout is required for a lot of features of iOS now. With iOS9 it's even more. – Fogmeister Jun 19 '15 at 12:23