I need a view that is 2" wide on any iPad regardless of orientation or which iPad device it is running on. How can I calculate a width for the view that will always deliver about 2" in width?
Asked
Active
Viewed 147 times
1
-
2" regardless of iPad model? You do know they come in different sizes? – sha Nov 07 '14 at 21:58
-
Yes, I know. Kind of strange,but I need the same absolute size for all devices. Doesn't have to be precise, just close. – RegularExpression Nov 07 '14 at 22:01
-
What kind of App are you trying to develop? – Unheilig Nov 07 '14 at 23:00
3 Answers
1
Using auto-layout create constraints for width and height. You might want to try and measure exact values for these constraints.
Also, here is question about detecting iPad Mini vs iPad Is it possible to detect that your iOS app is running on an iPad mini at runtime?, since you will need to change these values based on the model.
-
Sounds like an ideal way to approach it but I haven't learned auto-layout yet (I've started reading Erica Sadun's book on it, and my eyes quickly glazed over!) I plan to revisit it after I get this thing out the door. Thanks for the reply. – RegularExpression Nov 08 '14 at 05:21
1
You're going to have to convert points to pixels to inches. Points to pixels is easy and possible through the API:
pixels = [[UIScreen mainScreen] scale] * points
Converting to inches is a bit different. Have a look at this answer from a question about DPI (dots per inch):

Community
- 1
- 1

Ian MacDonald
- 13,472
- 2
- 30
- 51
-
This is the direction I was headed, so I think it is going to be my best shot. Thanks to everyone who replied. – RegularExpression Nov 08 '14 at 05:19
-3
Use NSLayoutConstraint, set a trailing space from your view to the super view. Irrespective of the device, the constraint follows its rule.

Anil Kumar
- 654
- 1
- 6
- 18
-
3What are you talking about? He asked about `width`, not the distance from the right edge? – sha Nov 07 '14 at 22:05
-
@sha hope you know what auto-layout is? and how it works, before downvoting an answer. If you set the width and add trailing space constraint to the superview, width will increase according to the space. – Anil Kumar Nov 07 '14 at 22:10
-
I do know about auto-layout. I don't understand how setting trailing edge would help to ensure specific width – sha Nov 07 '14 at 22:12
-
His requirement is to have a width proportionate to the device size, if I am not wrong. In this case, if there is a fixed trailing space the width of the view will increase/decrease according the device. – Anil Kumar Nov 07 '14 at 22:14
-
@AnilKumar Your answer doesn't solve the problem. The OP wants a view to be 2 inches (5.08 cm) regardless of the iPad device. Can a layout constraint handle physical measurements? – rmaddy Nov 07 '14 at 22:48