如何摆脱导航栏和phone顶部之间的space

How to get rid of the space between the navigation bar and the top of the phone

我没有使用导航控制器控制此视图,因为导航控制器强制我使用我不想使用的转场(而且我不确定如何重写这些转场,我试过多次事情,但它们总是导致崩溃);不过,使用导航控制器的好处之一是导航栏和 phone 顶部之间没有 space。

但是,因为我想使用我自己的自定义转场,所以我只是向我的视图控制器添加了一个导航栏。造成的问题如下:

导航栏和 phone 顶部之间有这个 space。而且我不希望那里有 space。有人知道如何解决这个问题吗?

您可以通过以下方式以编程方式创建导航栏:

// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account

navigationBar.barTintColor = UIColor.whiteColor()

// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Profile"

// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]

// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)

结果:

Space 已经不存在了。

原文Post:Adding Navigation Bar programmatically iOS

更新:

如果要添加按钮,请添加此代码:

// Create left and right button for navigation item
let leftButton =  UIBarButtonItem(title: "LeftButton", style:   UIBarButtonItemStyle.Plain, target: self, action: "leftClicked:")
let rightButton = UIBarButtonItem(title: "RightButton", style: UIBarButtonItemStyle.Plain, target: self, action: "rightClicked:")

// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton

这是辅助方法:

func leftClicked(sender: UIBarButtonItem) {
    // Do something
    println("Left Button Clicked")
}

func rightClicked(sender: UIBarButtonItem) {
    // Do something
    println("Right Button Clicked")
}

最终代码为:

import UIKit

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.sharedApplication().statusBarStyle = .LightContent
        // Create the navigation bar
        let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account

        navigationBar.barTintColor = UIColor.whiteColor()
        // Create a navigation item with a title
        let navigationItem = UINavigationItem()
        navigationItem.title = "Profile"

        // Create left and right button for navigation item
        let leftButton =  UIBarButtonItem(title: "LeftButton", style:   UIBarButtonItemStyle.Plain, target: self, action: "leftClicked:")
        let rightButton = UIBarButtonItem(title: "RightButton", style: UIBarButtonItemStyle.Plain, target: self, action: "rightClicked:")

        // Create two buttons for the navigation item
        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton

        // Assign the navigation item to the navigation bar
        navigationBar.items = [navigationItem]

        // Make the navigation bar a subview of the current view controller
        self.view.addSubview(navigationBar)
    }

    func leftClicked(sender: UIBarButtonItem) {
        // Do something
        println("Left Button Clicked")
    }

    func rightClicked(sender: UIBarButtonItem) {
        // Do something
        println("Right Button Clicked")
    }

}

首先,如果你使用独立 UINavigationBar 只是因为你不知道 segue 是如何工作的,我的第一个建议是使用 UINavigationController 因为它免费为你提供了很多.

根据 Apple 文档:

When you use a navigation bar as a standalone object, you are responsible for providing its contents. Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. A navigation item has properties for specifying views on the left, right, and center of the navigation bar and for specifying a custom prompt string.

如果您仍想探索此路径,则需要像这样实现 UINavigationBarDelegate-positionForBar:

func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
  return .TopAttached
}

您可能还需要设置 tintColor 以使其看起来与状态栏对齐:

let viewWidth = self.view.frame.size.width
var navBar: UINavigationBar = UINavigationBar(frame: CGRect(x:0, y:20, width: viewWidth, height:44))
navBar.barTintColor = UIColor .lightGrayColor()
navBar.delegate = self
self.view.addSubview(navBar)