I am building an App for the iPhone and iPad.The image sizes need to be completely different (doesn't have the same ratio). How can I achieve this situation? Is this something I can do in the xib file or do I need to achieve this with code? I only want to use one xib file, that is it.
-
The answer depends on what you want to do. Please show your current constraints, or storyboard layout, as well as what you expect the layout for both devices to look like. – Luke Van In Jul 14 '16 at 22:19
2 Answers
You could use a xib file and link your corresponding constraints to your code ( ctrl + drag
the constraints from the xib file to your swift or objective-C file ).
Then, you can adapt these constraints programmatically after detecting what kind of device your app is running on ( c.f. this answer : https://stackoverflow.com/a/10167255/3844377 ).
For instance, for your width constraint, you'll be able to do something like :
IBOutlet widthConstraint: NSLayoutConstraint!
func viewDidLoad()
{
super.viewDidLoad()
self.adaptConstraintsToDevice()
}
func adaptConstraintsToDevice()
{
if deviceIsIPad // check link above that code sample to check whether the app is running on an iPad or not
widthConstraint.constant = 100.5 // put your width value here
}
This can be done with size classes. As I have posted some screen shots
In First Image I have taken a UIImageView with fix (height+width) constraint, Top spacing and Horizotaly center.
In Second Image select the constraint width/Heght and From left menu click on (+) icon and add respective size class For ipad (Regular width, height-Any) [choose size class as per requirement] add the width and height value for that.
For More Info about size class and about size classes designs
Hope this will help you. Thanks

- 747
- 7
- 17