2015-09-18 61 views
1

我沒有使用導航控制器控制此視圖,因爲導航控制器迫使我使用我不想使用的segues(並且我'我不知道如何重寫這些賽段,我已經嘗試了很多東西,但總是會導致崩潰);儘管使用導航控制器的好處之一就是在導航欄和手機頂部之間沒有空間。如何擺脫導航欄和電話頂部之間的空間

但是,因爲我想使用我自己的自定義segues,我只是添加了一個導航欄到我的視圖控制器。這導致的問題如下:

enter image description here

有導航欄和手機的頂部之間的空間。我不想要那裏的空間。任何人都知道如何解決這個問題?

+0

您已添加自定義導航欄,這就是創建此問題的原因。 –

+0

@E隊長我不一定希望隱藏它。我只想讓酒吧和電話之間的空間消失。 –

+0

如果要添加自定義導航,請將條的Y位置更改爲20。因爲狀態欄大小是20. –

回答

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

UPDATE:

如果你想添加按鈕添加以下代碼:

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

這裏是helper方法:

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") 
    } 

} 
+0

@iAshish因爲那時配置文件將會太接近時間。他們會看起來很沮喪。 –

+0

@Dharmesh Kheni但手機和導航欄之間仍有空間...我希望載體,時間和電池壽命與導航欄顏色完全相同。 –

+0

得到它的工作!我現在如何放置按鈕? –

0

首先,如果你使用的是獨立的UINavigationBar只是因爲你可能不知道如何賽格瑞的作品,我第一次的建議是使用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) 
+0

感謝您指出@iAshish。我沒看到標籤:)。更正了我的代碼! – Abhinav

+0

歡迎。請改正你的寬度320也不好。 iPhone 6,6 Plus,6s和6s Plus目前在市場上銷售:) –

+1

錯過了趕時間修復它:)。 @iAshish – Abhinav