【问题标题】:iOS 8 Swift navigation bar title, buttons not showing in tab based applicationiOS 8 Swift 导航栏标题,按钮未显示在基于选项卡的应用程序中
【发布时间】:2015-10-04 20:10:41
【问题描述】:

我正在尝试关注 this tutorialthis answerthis answer 在 iOS 8 / Swift 中基于选项卡的应用程序中为我的每个选项卡创建导航栏,但导航栏上没有标题或按钮正在显示。

这是我目前所拥有的:

// AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {            
    let tabBarController = UITabBarController()

    let vc1 = ViewController()
    let vc2 = ViewController()
    let vc3 = ViewController()

    let nc1 = UINavigationController(rootViewController: vc1)
    let nc2 = UINavigationController(rootViewController: vc2)
    let nc3 = UINavigationController(rootViewController: vc3)

    let controllers = [nc1,nc2,nc3]

    tabBarController.viewControllers = controllers

    nc1.tabBarItem = UITabBarItem(title: "item1", image: nil, tag: 1)
    nc2.tabBarItem = UITabBarItem(title: "item2", image: nil, tag: 1)
    nc3.tabBarItem = UITabBarItem(title: "item3", image: nil, tag: 1)

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.rootViewController = tabBarController
    window?.makeKeyAndVisible()

    return true
}

// ViewController.swift
class ViewController: UIViewController, UINavigationBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
        navigationBar.backgroundColor = UIColor.blueColor()
        navigationBar.delegate = self;

        let navigationItem = UINavigationItem()
        navigationItem.title = "Title"

        let leftButton =  UIBarButtonItem(title: "Left Button", style:   UIBarButtonItemStyle.Plain, target: self, action: nil)
        let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton

        navigationBar.items = [navigationItem]

    }

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }
}

但我只是在模拟器顶部看到一个空白导航栏。

【问题讨论】:

  • 标题你试过了吗self.title = "Your Title"
  • 看起来我的导航栏部分位于状态栏下方。当我使用尺寸时,我可以看到标题和按钮伸出底部。
  • 阿拉丁,你走对了。我正在创建两个导航栏。导航控制器带有导航栏,我需要使用 self.navigationItem.title = "My Title" 设置标题。
  • 这就是我无法理解的,我想知道您为什么要创建一个新的导航栏,而您的视图控制器已经嵌入到一个已经有栏的导航控制器中

标签: ios iphone swift uinavigationcontroller uitabbarcontroller


【解决方案1】:

您正在制作自定义 UINavigationBar,而 UINavigationController 已经提供给您。

在您的 ViewController 中试试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Title"

    let navigationBar = navigationController!.navigationBar
    navigationBar.tintColor = UIColor.blueColor()

    let leftButton =  UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

    navigationItem.leftBarButtonItem = leftButton
    navigationItem.rightBarButtonItem = rightButton
}

【讨论】:

    【解决方案2】:

    无需使用新的导航栏,只需使用现有的导航栏即可。记住你在这里做了什么:

    let nc1 = UINavigationController(rootViewController: vc1)
    

    所以您的视图控制器已经嵌入到导航控制器中,只需使用 self 访问导航项

    self.title = "Your Title"
    
    var homeButton = UIBarButtonItem(title: "LeftButton", style: .Plain, target: self, action: "")
    
    var logButton = UIBarButtonItem(title: "RigthButton", style: .Plain, target: self, action: "")
    
    self.navigationItem.leftBarButtonItem = homeButton
    self.navigationItem.rightBarButtonItem = logButton
    

    【讨论】:

      【解决方案3】:

      我不必要地创建了两个导航栏。导航控制器自带导航栏,我把我的ViewController改成:

      class ViewController: UIViewController, UINavigationBarDelegate {
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              self.navigationItem.title = "My Title"
              self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style:   UIBarButtonItemStyle.Plain, target: self, action: nil)
              self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
          }
      
          func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
              return UIBarPosition.TopAttached
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 1970-01-01
        • 2013-04-12
        相关资源
        最近更新 更多