Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何消除导航栏和手机顶部之间的空间_Ios_Iphone_Swift_Uinavigationbar - Fatal编程技术网

Ios 如何消除导航栏和手机顶部之间的空间

Ios 如何消除导航栏和手机顶部之间的空间,ios,iphone,swift,uinavigationbar,Ios,Iphone,Swift,Uinavigationbar,我没有使用导航控制器控制此视图,因为导航控制器强制我使用我不想使用的分段(我不知道如何重写这些分段,我尝试了多种方法,但它们总是导致崩溃);尽管如此,使用导航控制器的好处之一是导航栏和手机顶部之间没有空间 但是,因为我想使用自己的自定义segues,所以我只是在视图控制器中添加了一个导航栏。造成的问题如下: 导航栏和手机顶部之间有这个空间。我也不想要那里的空间。有人知道如何解决这个问题吗?您可以通过以下方式编程创建导航栏: // Create the navigation bar let na

我没有使用导航控制器控制此视图,因为导航控制器强制我使用我不想使用的分段(我不知道如何重写这些分段,我尝试了多种方法,但它们总是导致崩溃);尽管如此,使用导航控制器的好处之一是导航栏和手机顶部之间没有空间

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


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

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

// 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)
结果:

空间不再存在了

原职:

更新:

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

// 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
,因为它免费为您提供了很多

根据苹果文档:

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

如果仍要探索此路径,则需要实现
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)

您添加了自定义导航栏,这就是创建此问题的原因。@El CaptainI不一定要将其隐藏。我只想去掉酒吧和手机之间的空间。如果要添加自定义导航,请将酒吧的Y位置更改为20。因为状态栏的大小是20。导航必须是64px而不是44。@iAshish,因为这样配置文件就太接近时间了。它们看起来会很脏。@Dharmesh Kheni但在手机和导航栏之间还有一个空间。。。我希望载体的位置、时间和电池寿命与导航栏的颜色相同。哈哈。只是开玩笑。我会先做我的研究。检查我更新的代码。希望这就是你需要的…:)我刚刚将你提供的一个参数,即44改为60。它为我改变了它。谢谢你指出@iAshish。我没有看到标签:)。更正了我的代码!欢迎请纠正你的宽度太多,只是320不好。iphone6、6plus、6s和6s Plus现在已经上市:)在匆忙修复时错过了这一点:)@亚希什