11

我的应用程序已经在应用商店中,昨天我已经将我的 Xcode 版本更新到 9 并且除了iPhone x之外工作正常。UI 崩溃了。

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

2.为UIViewController添加扩展,用于制作HeaderTab bar

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

    }

并像下面这样调用每个Viewcontroller

self.addHeaderTab(currentViewController: self, content:"நிகழ்ச்சி நிரல்" , isMenu: true)

就像那个标签栏一样,我也做过,但所有设备都可以正常工作,但需要iPhone x

请参阅我的屏幕截图:

图片

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

但不清楚他们的文件。

帮助将不胜感激,在此先感谢。

4

4 回答 4

23

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

安全区域是屏幕上未被凹槽或主页指示器重叠的区域。

在此处输入图像描述

为避免您遇到的问题,您必须更改 noView 对 iOS11 的最高约束:

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)
])

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

在此处输入图像描述 在此处输入图像描述

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

在此处输入图像描述 在此处输入图像描述

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

于 2017-09-21T21:02:26.097 回答
6

在 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];

}
于 2017-10-04T11:12:22.553 回答
0

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

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

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

于 2017-12-08T09:19:45.057 回答
-3

根据设备设置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
}

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

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

    // portrait orientation - status bar is shown
    additionalSafeAreaInsets.top = 24.0
于 2018-05-08T11:32:48.510 回答