Reputation: 683
I want to change the size of UIText, UIButton texts according to device. I tried to use size classes by defining different size for different device types.
E.g. To define size for iPhones, I did the following steps:
Choosed size class wCompact hRegular
Added "wCompact hRegular" class by clicking the + icon at the bottom of the attribute inspector. (Just before installed button)
Added required size by adding size for "wCompact hRegular" in attribute inspector.
But still I cant get the defined size for iPhone instead the size defined for "wAny hAny" has also gone.
Can someone tell me what is wrong with above process or give some good written tutorial for using size classes.
Note: I followed http://swiftiostutorials.com/using-size-classes-xcode-6/ link but I couldn't get the output as mentioned in that post.
Upvotes: 0
Views: 169
Reputation: 1536
Size classes are more appropriate for adapting to different device orientations eg. from landscape to portrait not for different size screens. In most cases you will be able to adapt for screen sizes with layout constraints. Try to focus on the intention of the layout rather the number of pixels a UIButton should have as the width. For example a UIButton may be left aligned to the superview then right aligned to a UITextField etc. Ultimately the dream is to create layouts that can adapt to different size screens without you needing to change anything for different screen sizes.
If you still really want to set specific sizes for different device sizes then set up constraints on the adaptable elements and change the constants with something like this. I'm not sure if there is more elegant way to achieve this.
switch UIScreen.mainScreen().bounds.size.width{
case kIphone6PlusWidth:
buttonWidthConstraint.constant = 120
case kIphone6Width:
buttonWidthConstraint.constant = 100
default:
buttonWidthConstraint.constant = 80
}
Upvotes: 0