ios - 无法设置 NavigationItem 的 TintColor

标签 ios swift uinavigationitem

我的情况:

  • 有一个 View_A (UICollectionViewController&UICollectionViewCell) 和 View_B (UIViewController)。当我使用 StoryBoard 中的 Segue 触摸 View_A 中的一个单元格时,我想切换到 View_B。

  • StoryBoard 中,我将 View_A 和 View_B 连接到标识符为 SegueToView_BPush Segue

  • switchViews 的功能运行良好。

我的问题:

  • 有了 Push Segue,我不需要添加 BackButton (NavigationItem) 来返回 View_A,因为有一个“NavigationItem”由系统自动装箱。我尝试了其他类型的转场,例如 ModalPopover,并且 NavigationItem 不是自动创建的。我想问为什么?

  • 我想为系统自动创建的那个NavigationItem设置特定的颜色,而不是默认的蓝色,但我没找到。之后,我只是在 prepareForSegue() 中设置了颜色,但它不起作用。请告诉如何设置它的具体颜色?

我的代码:

 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.collectionView?.setPresenting(true, animated: true, completion: nil)
    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    self.selectedCard = delegate.otherCards[indexPath.row]
    self.performSegueWithIdentifier("SegueToView_B", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        if identifier == "SegueToView_B" {
            let myOtherCardViewController = segue.destinationViewController as? View_BViewController
            myOtherCardViewController!.otherCard = self.selectedCard
            myOtherCardViewController!.navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()  // Failed to work!!!
            myOtherCardViewController?.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()  // Failed to work, too!!!
        }
    }
}

感谢您的帮助。

伊桑·乔

最佳答案

为该导航栏设置 tintColor:

myOtherCardViewController.navigationBar.tintColor = .whiteColor()

为什么使用Modal或PopOver时没有Navigationbar?因为这就是 Modal 和 Popover 的工作方式!您必须为与模态转场连接的 View 创建另一个导航 Controller ,如下所示: enter image description here

我使用的另一种技术是,创建单个 NavigationController 类,并设置所有需要的属性(颜色、字体等),然后将 Storyboard 中的所有 NavigationController 链接到该 NavigationController 类。

有了它,您就不必重新配置每个 NavigationController。

关于ios - 无法设置 NavigationItem 的 TintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29959821/

相关文章:

ios - XCUITest 等待其中包含特定静态文本的单元格

iphone - 编写自己的 UINavigationController?类似的功能,而不是子类化

objective-c - 如何检索和解析大量的 XML 数据?

ios - 在 View 上自定义多个手势

ios - 从 UIPickerview 获取文本并在 UIAlert 中移动 Textfield

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

ios - 更改自定义 UINavigationBar 属性不能完全快速工作

swift - iOS 13 UINavigationBar 在 UINavigationItem 变化时不会自动计算大小

ios - 根据 if 语句更改 TableView 尺寸

Swift Regex 捕获括号之间的文件名?