Reputation: 595
My Xcode product was developed on Xcode 10, using the iPhone 11 template and all the view controllers are perfectly aligned.
To make my app more diverse I need to change the size of buttons etc but when I switch the device to say an iPhone 8 or iPhone 4 and change the constraints and switch back to the iPhone 11 view the images are working off the new constraints even though I used the 'vary traits' button.
Upvotes: 1
Views: 140
Reputation: 103
well you can change or set constraint by using this below code by using it in your class so, you can set your view for iphone and ipad.
struct Device {
// iDevice detection code
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_RETINA = UIScreen.main.scale >= 2.0
static let SCREEN_WIDTH = Int(UIScreen.main.bounds.size.width)
static let SCREEN_HEIGHT = Int(UIScreen.main.bounds.size.height)
static let SCREEN_MAX_LENGTH = Int( max(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let SCREEN_MIN_LENGTH = Int( min(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let IS_IPHONE_4_OR_LESS = IS_IPHONE && SCREEN_MAX_LENGTH < 568
static let IS_IPHONE_5 = IS_IPHONE && SCREEN_MAX_LENGTH == 568
static let IS_IPHONE_6 = IS_IPHONE && SCREEN_MAX_LENGTH == 667
static let IS_IPHONE_6P = IS_IPHONE && SCREEN_MAX_LENGTH == 736
static let IS_IPHONE_X = IS_IPHONE && SCREEN_MAX_LENGTH == 812
}
now you can set constrain like this:==
if(Device.IS_IPHONE_5 || Device.IS_IPHONE_4_OR_LESS){
//--- set your constrain for iphone 5 and 4
}else if(Device.IS_IPAD){
//--- set your constrain for ipad
}else{
//--- set default constrain
}
Upvotes: 0
Reputation: 345
I don't know if there is another way to do this, but I get the device screen and change the size for me works changing the biggest one's and smallest one's screens, but you can create a list of sizes for each different resolution
extension UIDevice {
var hasNotch: Bool {
if #available(iOS 11.0, *) {
let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
return bottom > 0
} else {
return false
}
}
public var iPhoneX: Bool {
return UIScreen.main.nativeBounds.height == 2436
}
public var iPhone: Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}
public var smallScreen: Bool {
return screenType == .iPhones_5_5s_5c_SE || screenType == .iPhones_4_4S
}
public enum ScreenType: String {
case iPhones_4_4S = "iPhone 4 or iPhone 4S"
case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
case iPhones_X_XS = "iPhone X or iPhone XS"
case iPhone_XR = "iPhone XR"
case iPhone_XSMax = "iPhone XS Max"
case unknown
}
public var screenType: ScreenType {
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhones_4_4S
case 1136:
return .iPhones_5_5s_5c_SE
case 1334:
return .iPhones_6_6s_7_8
case 1792:
return .iPhone_XR
case 1920, 2208:
return .iPhones_6Plus_6sPlus_7Plus_8Plus
case 2436:
return .iPhones_X_XS
case 2688:
return .iPhone_XSMax
default:
return .unknown
}
}
public var buttonsizeheight: Int {
switch UIScreen.main.nativeBounds.height {
case 960:
return 10
case 1136:
return 20
case 1334:
return 25
case 1792:
return 28
case 1920, 2208:
return 30
case 2436:
return 33
case 2688:
return 35
default:
return 20
}
}
}
Upvotes: 1