iOS 11 iPhone x 安全区域布局指南

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46344381/
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-08-31 09:30:34  来源:igfitidea点击:

iOS 11 Layout guidance about safe Area for iPhone x

iosautolayoutswift4xcode9iphone-x

提问by karthikeyan

My application already in app store, yesterday i have updated my Xcode version to 9 and works fine except iPhone x. UI got collapsed.

我的应用程序已经在应用程序商店中,昨天我已将我的 Xcode 版本更新为 9,并且除iPhone x外都可以正常工作。用户界面崩溃了。

1.Here I have Created Two UIView(both are fixed height) named as Header and Tab bar and I have hidden my NavigationBarentire app.

1.在这里我创建了两个UIView(都是固定高度),分别命名为 Header 和 Tab bar,并且我隐藏了我的NavigationBar整个应用程序。

2.Added extension to UIViewControllerfor making Headerand Tab bar

2.为UIViewController添加了用于制作HeaderTab栏的扩展

func addHeaderTab(currentViewController:UIViewController,content:String, isMenu:Bool){
        let noView = TopHeaderView()
        noView.tag = 12345
        noView.isMenu = isMenu
        noView.translatesAutoresizingMaskIntoConstraints = false
        currentViewController.view .addSubview(noView)
        if isMenu{
            noView.menuBtn .setImage(UIImage(named: "Small"), for: .normal)
            noView.logoImg.isHidden = false

        }else{
            noView.menuBtn .setImage(UIImage(named: "arrow_small"), for: .normal)
            noView.logoImg.isHidden = true
        }
        noView.titleLbl.text = content
        noView.delegate = (currentViewController as! menuOpen)

        NSLayoutConstraint(item: noView, attribute: .leading, relatedBy: .equal, toItem: currentViewController.view, attribute: .leading, multiplier: 1.0, constant: 0.0).isActive = true

        NSLayoutConstraint(item: noView, attribute: .trailing, relatedBy: .equal, toItem: currentViewController.view, attribute: .trailing, multiplier: 1.0, constant: 0.0).isActive = true

        NSLayoutConstraint(item: noView, attribute: .top, relatedBy: .equal, toItem: currentViewController.view, attribute: .top, multiplier: 1.0, constant: 0.0).isActive = true

        NSLayoutConstraint(item: noView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64).isActive = true

    }

and calling this every Viewcontrollerlike below :

并在每个Viewcontroller 中调用它,如下所示:

self.addHeaderTab(currentViewController: self, content:"????????? ?????" , isMenu: true)

Like that Tab bar also i have done but all the device working expect iPhone x.

像那个 Tab 栏一样,我也做过,但所有设备都可以正常工作,期待iPhone x

See My screen shot :

请参阅我的屏幕截图:

Image

图片

i have studied about https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/

我研究过https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/

but am not clear with their document.

但不清楚他们的文件。

Help would be appreciated, Thanks in advance.

帮助将不胜感激,提前致谢。

回答by joern

With iOS11 (and the appearance of iPhoneX) Apple introduced the Safe Area Layout Guideto adapt your views to the iPhoneX.

在 iOS11(以及 iPhoneX 的出现)中,Apple 引入了安全区域布局指南来使您的视图适应 iPhoneX。

The Safe Area is the area of the screen that is not overlapped by the notch or the home indicator.

安全区域是屏幕上没有被缺口或主页指示器重叠的区域。

enter image description here

在此处输入图片说明

To avoid the issue you are experiencing you have to change your noView's top constraint for iOS11:

为了避免您遇到的问题,您必须更改 iOS11 的 noView 顶部约束:

if #available(iOS 11, *) {
   let guide = view.safeAreaLayoutGuide
   NSLayoutConstraint.activate([
      noView.topAnchor.constraint(equalTo: guide.topAnchor)
   ])
} else {
   NSLayoutConstraint.activate([
      noView.topAnchor.constraint(equalTo: currentViewController.topLayoutGuide.bottomAnchor)
   ])
}
NSLayoutConstraint.activate([
   noView.leadingAnchor.constraint(equalTo: currentViewController.view.leadingAnchor),
   noView.trailingAnchor.constraint(equalTo: currentViewController.view.trailingAnchor),
   noView.heightAnchor.constraint(equalToConstant: 65)
])

Unfortunately that is not all. Because now your noViewmoves down on iPhone X, but the status bar does not have a red background anymore. You have add a red background color behind the status bar:

不幸的是,这还不是全部。因为现在您noView在 iPhone X 上向下移动,但状态栏不再有红色背景。您在状态栏后面添加了红色背景色:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

You could make things a lot easier by using a UINavigationController(with a red navigation bar):

通过使用UINavigationController(带有红色导航栏),您可以使事情变得更容易:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

With this approach you don't have to set any constraints! The systems does all the adjustments for you automatically.

使用这种方法,您不必设置任何约束!系统会自动为您进行所有调整。

回答by Chandan

In Objective-C for top and bottom margin when on iPhone-X

在 iPhone-X 上,在 Objective-C 中用于顶部和底部边距

if (@available(iOS 11, *)) {

    NSLayoutConstraint *bottomConstraint   = [NSLayoutConstraint constraintWithItem:self.childView
                                                                          attribute:NSLayoutAttributeBottom
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:self.parentView.safeAreaLayoutGuide
                                                                          attribute:NSLayoutAttributeBottom
                                                                         multiplier:1.0
                                                                           constant:0];


    NSLayoutConstraint *topConstraint   = [NSLayoutConstraint constraintWithItem:self.childView
                                                                   attribute:NSLayoutAttributeTop
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.parentView.safeAreaLayoutGuide
                                                                   attribute:NSLayoutAttributeTop
                                                                  multiplier:1.0
                                                                    constant:0];


} else {

    NSLayoutConstraint *bottomConstraint   = [NSLayoutConstraint constraintWithItem:self.childView
                                                                      attribute:NSLayoutAttributeBottom
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.parentView
                                                                      attribute:NSLayoutAttributeBottom
                                                                     multiplier:1.0
                                                                       constant:0];


    NSLayoutConstraint *topConstraint   = [NSLayoutConstraint constraintWithItem:self.childView
                                                                   attribute:NSLayoutAttributeTop
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.parentView
                                                                   attribute:NSLayoutAttributeTop
                                                                  multiplier:1.0
                                                                    constant:0];

}

回答by Chandan

If you don't want to use a UINavigationControllerbut you want to have a navigation bar, you can do this way (using a UIImageView):

如果你不想使用 aUINavigationController但你想要一个导航栏,你可以这样做(使用 a UIImageView):

https://medium.com/@kahseng.lee123/creating-custom-navigation-bar-tab-bar-for-iphone-x-f03b1e1827d3

https://medium.com/@kahseng.lee123/creating-custom-navigation-bar-tab-bar-for-iphone-x-f03b1e1827d3

Or you can create a view and set its top constraint to the top of the superview and its bottom contraint to the top of the navigation bar. Don't forget to make it red. :-)

或者您可以创建一个视图并将其顶部约束设置为超级视图的顶部,并将其底部约束设置为导航栏的顶部。别忘了把它变成红色。:-)

回答by Mehul Sojitra

Set height constrain of headerView according to the device.

根据设备设置 headerView 的高度约束。

if iPhoneX {
NSLayoutConstraint(item: noView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64+24).isActive = true
} else {
NSLayoutConstraint(item: noView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64).isActive = true
}

And set all the subView's(of headerView) constrain respective to bottom of superview(headerView).

并将所有子视图(headerView)的约束分别设置为超级视图(headerView)的底部。

Additional note on why we have added 24 pixel for iphoneX:

关于我们为 iphoneX 添加 24 像素的原因的附加说明:

    // portrait orientation - status bar is shown
    additionalSafeAreaInsets.top = 24.0