"IOS" screen fit Nslayoutconstraint

Source: Internet
Author: User


Objective

How to achieve a picture on the iphone and ipad display different sizes, I learned that there are three ways: direct handwriting code dynamically add constraints; Nslayoutconstraint associated to the Viewcontroller viewdidload inside plus judgment assignment , with size classes (this is not very good at present). Here is a way to share a direct fit within the storyboard.

StatementWelcome reprint, but please keep the original source of the article:)Blog Park: http://www.cnblogs.comFarmer Uncle: Http://over140.cnblogs.com

Body

Import Foundation

classNslayoutconstraintex:nslayoutconstraint {

@IBInspectable
var ipad:cgfloat =0{
Didset {
ifDeviceutils.isipad () {
Constant = ipad
}
}
}

@IBInspectable
var iphone4:cgfloat =0{
Didset {
//640 x 960
ifDeviceutils.isiphone () && deviceutils.getscreensize (). Height = =960.0{
Constant = Self.iphone4
}
}
}

@IBInspectable
var iphone5:cgfloat =0{
Didset {
//640 x 1136
ifDeviceutils.isiphone () && deviceutils.getscreensize (). Height = =1136.0{
Constant = Self.iphone5
}
}
}

@IBInspectable
var iphone6:cgfloat =0{
Didset {
//x 1334
ifDeviceutils.isiphone () && deviceutils.getscreensize (). Height = =1334.0{
Constant = Self.iphone6
}
}
}

@IBInspectable
var iphone6plus:cgfloat =0{
Didset {
//1242 x 2208
ifDeviceutils.isiphone () && deviceutils.getscreensize (). Height >=1920.0{
Constant = Self.iphone6plus
}
}
}
}

Usage:

Modify the current constraint so that it uses Nslayoutconstraintex, with the following effect:

Note that the user Defined Runtime attributes this column, if you remove a custom attribute, there may be a residue on this side, delete the remaining item can be removed. and the module column. If the show none indicates that using a custom class fails, it would be nice to remove the class and re-enact it.

The default is the iphone layout, so you can use a large size image on your ipad.

End

Bought this auto layout development cheats, continue to learn and think about screen adaptation aspects of the content.

"IOS" screen fit Nslayoutconstraint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.