[英]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.