xcode iOS 对不同设备的不同约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48651057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:32:35  来源:igfitidea点击:

iOS different constraints for different devices

iosxcodesize-classes

提问by xskit

I have a ViewController designed for iPhone SE

我有一个专为 iPhone SE 设计的 ViewController

enter image description here

在此处输入图片说明

As you can see I also have a constraint Align Top to: Safe Area Equals 75

正如你所看到的,我也有一个约束 Align Top to: Safe Area Equals 75

The question is, is it possible to change this value for iPhone 8 and iPhone 8 Plus? For example:

问题是,是否可以为 iPhone 8 和 iPhone 8 Plus 更改此值?例如:

  • SE = 75
  • 8 = 85
  • 8 Plus = 105
  • SE = 75
  • 8 = 85
  • 8 加 = 105

采纳答案by Jakub Pr??a

we were facing similar issue. We solved with helper class called scaled. Which basically just multiply size of something, which should appear larger on larger device.

我们面临着类似的问题。我们使用名为 scaled 的辅助类解决了问题。这基本上只是乘以某些东西的大小,在较大的设备上应该显得更大。

extension CGFloat {
    public var scaled: CGFloat {
        switch UIDevice.type.getResolutionGroup()! {
        case .lr320x568:
            return self
        case .lr375x667:
            return self * 1.1
        case .lr414x736:
            return self * 1.2
        case .lr768x1024:
            return self * 1.3
        // For iPads
        case .lr1024x1366:
            return self * 1.3
        }
    }
}

And implementation of resolution group

和执行决议组

public func getResolutionGroup() -> ResolutionGroup? {
        switch self {
        case .iPhone5, .iPhone5C, .iPhone5S, .iPhoneSE, .iPodTouch5, .iPodTouch6:
            return .lr320x568
        case .iPhone6, .iPhone6S, .iPhone7:
            return .lr375x667
        case .iPhone6Plus, .iPhone6SPlus, .iPhone7Plus:
            return .lr414x736
        case .iPadMini, .iPadMini2, .iPadMini3, .iPadMini4:
            return .lr768x1024
        case .iPad2, .iPad3, .iPad4, .iPadAir, .iPadAir2:
            return .lr768x1024
        case .iPadPro:
            return .lr1024x1366
        case .simulator:
            return isiPhone() ? .lr320x568 : .lr768x1024
        default:
            return .lr320x568
        }
    }

And usage in app

以及在应用程序中的使用

fileprivate let footerHeight = CGFloat(180).scaled

回答by tryKuldeepTanwar

Easy way!

简单的方法!

To overcome this issue I created a small libraryso you don't have to write a single line of code just assign the class (NSLayoutHelper) to your constraint and you'll be able to update your constraint for all the devices differently.

为了解决这个问题,我创建了一个小型库,这样您就不必编写一行代码,只需将类 (NSLayoutHelper) 分配给您的约束,您就能够以不同的方式更新所有设备的约束。

enter image description here

在此处输入图片说明

For updating constraints

用于更新约束

enter image description here

在此处输入图片说明

Output

输出

enter image description here

在此处输入图片说明

回答by andlin

Not using Interface Builder, no. Constraints, can only target Compact, Regular or Any sizes and all iPhone models have Compact width and Regular height when in portrait mode.

不使用界面生成器,不。限制,只能针对紧凑型、普通型或任意尺寸,并且所有 iPhone 机型在纵向模式下都具有紧凑型宽度和普通高度。

If you want that kind of granularity, you have to do it with code instead.

如果你想要那种粒度,你必须用代码来做。

回答by Sh_Khan

This is not applicanle in IB , you can try in code by hooking the top constraint of the view as IBOutlet and in viewDidLayoutSubviews

这在 IB 中不适用,您可以通过将视图的顶部约束挂钩为 IBOutlet 并在 viewDidLayoutSubviews 中尝试在代码中

override func viewDidLayoutSubviews()
{

   if(deviceWidthSE)
   {      
      self.viewTopCon.constant = 75
   }
   else
   if(deviceWidth8)
   {
      self.viewTopCon.constant = 85
   }
   else
   if(deviceWidth8Plus)
   {
      self.viewTopCon.constant = 105
   }

}