位置导航栏顶部的安全区域

问题描述 投票:0回答:4

我升级到新的iPhone并升级我的应用程序。我添加了x / xr / xs布局,所以黑色边框现在消失了。

我的视图顶部有一个导航栏,但现在它已经过多地进入状态栏?我该如何解决?以编程方式而不是故事板(学习代码)。

一些代码:

func buildTopbar()
{
    statusbar = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 20));

    statusbar.backgroundColor = UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1.0);

    navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 44));

    navigationbar.barTintColor = UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1.0);

    let texta = UIBarButtonItem(title: "A", style: .plain, target: self, action: #selector(settingToolbar(sender:)));
    let textb = UIBarButtonItem(title: "B", style: .plain, target: self, action: #selector(inviteToolbar(sender:)));

    texta.tintColor = ColorHelper.primaryColor();
    textb.tintColor = ColorHelper.primaryColor();

    ...

    navigationbar.setItems([title], animated: false);

    self.view.addSubview(statusbar);
    self.view.addSubview(navigationbar);
}

我将状态栏设置为20,这就是旧手机中的状态栏。

ios iphone swift swift4.1 safearealayoutguide
4个回答
1
投票

我建议创建一个UINavigationController并将控制器添加到该导航控制器,它会自动调整导航栏。

如果您想手动添加导航栏。您必须将其框架Y位置设置为右侧。

计算导航栏的起始Y位置

let startingYPos = UIApplication.sharedApplication.statusBarFrame.size.height;
navigationbar = UINavigationBar(frame: CGRect(x: 0, y: startingYPos, width: self.view.bounds.width, height: 44));

1
投票

我的建议是为top添加一些约束。并根据你的iPhone设备大小或iOS版本设置约束值。最近我通过设置这样来实现它。我的问题是iOS版本,我的视图接近状态栏。所以我以编程方式设置了一些约束并处理它。这样它就不会影响iphone 7下面的iphone屏幕。

In Viewdidload()
     if #available(iOS 11, *)  {
            }
            else{
                topConstraints.constant = 20
            }

在这里,我检查了版本。您还可以检查设备,然后可以设置约束。如果您想要进行设备检查,请与我们联系。

希望它能起作用!!


0
投票

状态栏的高度现在从20pt变为44pt(从iPhone X开始)。如果要使用现有代码,则需要对状态栏高度应用特定于设备的检查。

这是一篇更详细解释导航栏布局(适用于iPhone X及更高版本设备)的文章:

What is the top bar height of iPhone X?


0
投票

我建议你把UIScreen的高度放在一个条件下:

if UIScreen.main.bounds.size.height > 750 {
        navigationBar  = UINavigationBar(frame: CGRect(x: 0, y: 43, width: view.frame.size.width, height: 44))
    }
    else{
        navigationBar  = UINavigationBar(frame: CGRect(x: 0, y: 20, width: view.frame.size.width, height: 44))
    }

如果您的视图具有滚动视图并且其高度设置为超过默认值,则可能无效,请尝试: -

view.bounds.size.height

© www.soinside.com 2019 - 2024. All rights reserved.