ios - 将导航栏放置在安全区域顶部

标签 ios iphone swift swift4.1 safearealayoutguide

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

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

一些代码:

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,这与旧手机中的状态栏相同。

最佳答案

我建议创建一个 UINavigationController 并将您的 Controller 添加到该导航 Controller ,它将自动调整导航栏。

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

计算导航栏的起始 Y 位置

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

关于ios - 将导航栏放置在安全区域顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53607039/

相关文章:

ios - 某些字体没有显示?

ios - Uber iOS SDK - 如何实现自定义 Uber 叫车请求按钮

ios - 无法在 UIWebView 的任何输入文本字段或文本区域中选择文本

xcode - 如何使用 Swift 检测充电器是否插入或拔出?

iphone - UIToolBar 意外地在 UIBarButtonItem 实例上注册点击,即使在距它们相当远的地方点击也是如此

iphone - 以模态方式呈现 UIImagePickerController 时出错

iphone - 从我的 iPhone 应用程序将照片附加到电子邮件

ios - 当键盘出现时 View 仅大于屏幕时,如何在 iOS 中滚动?

ios - UIButton 在 collectionView 单元格中不起作用

ios - UITextField 编辑更改不调用