如何摆脱导航栏和手机顶部之间的空间

时间:2015-09-18 05:56:28

标签: ios iphone swift uinavigationbar

我没有使用导航控制器来控制这个视图,因为导航控制器强迫我使用我不想使用的segues(而且我不确定如何过度编写这些segues,我已经' ve尝试了多件事,但他们总是导致崩溃);尽管如此,使用导航控制器的好处还在于导航栏和手机顶部之间没有空间。

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

enter image description here

导航栏和手机顶部之间有这个空间。而且我不想要那里的空间。有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

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

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

结果:

enter image description here

空间不再存在了。

原帖: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")
    }

}

答案 1 :(得分:0)

首先,如果您使用独立UINavigationBar只是因为无法弄清楚segue的工作方式,我的第一个建议就是使用UINavigationController,因为它可以免费提供给您。 / p>

Per Apple Docs:

  

当您使用导航栏作为独立对象时,您就是   负责提供其内容。与其他类型的观点不同,   您不直接将子视图添加到导航栏。相反,你使用   一个导航项(UINavigationItem类的一个实例)   指定要显示的按钮或自定义视图。导航   item具有用于在左侧,右侧和右侧指定视图的属性   导航栏的中心和用于指定自定义提示   字符串。

如果您仍想浏览此路径,则需要实现-positionForBar:的{​​{1}},如下所示:

UINavigationBarDelegate

您可能还必须设置func positionForBar(bar: UIBarPositioning) -> UIBarPosition { return .TopAttached } 以使其与状态栏看起来一致:

tintColor
相关问题