簡體   English   中英

如何擺脫導航欄和手機頂部之間的空間

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

我沒有使用導航控制器來控制此視圖,因為導航控制器強迫我使用不想使用的腳本(而且我不確定如何覆蓋這些腳本,我已經嘗試了多種方法,但是它們總是會導致崩潰); 不過,除了其他功能外,使用導航控制器的好處是,導航欄和手機頂部之間沒有空間。

但是,因為我想使用自己的自定義序列,所以我只在視圖控制器中添加了一個導航欄。 造成的問題如下:

在此處輸入圖片說明

導航欄和手機頂部之間有此空間。 我不希望那里有空間。 有人知道如何解決這個問題嗎?

您可以通過以下方式以編程方式創建導航欄:

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

結果:

在此處輸入圖片說明

空間不再存在。

原始文章:以編程方式添加導航欄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")
    }

}

首先,如果僅由於無法確定segue的工作方式而使用獨立的UINavigationBar ,我的第一個建議就是使用UINavigationController因為它免費提供了很多。

每個Apple文件:

將導航欄用作獨立對象時,您有責任提供其內容。 與其他類型的視圖不同,您不會將子視圖直接添加到導航欄中。 而是使用導航項(UINavigationItem類的實例)來指定要顯示的按鈕或自定義視圖。 導航項的屬性用於在導航欄的左側,右側和中心指定視圖,並用於指定自定義提示字符串。

如果你還是想探索這條道路,那么你需要實現-positionForBar:對的UINavigationBarDelegate是這樣的:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM