我的情况:
有一个 View_A (
UICollectionViewController
&UICollectionViewCell
) 和 View_B (UIViewController
)。当我使用StoryBoard
中的Segue
触摸 View_A 中的一个单元格时,我想切换到 View_B。在
StoryBoard
中,我将 View_A 和 View_B 连接到标识符为SegueToView_B
的Push Segue
。switchViews 的功能运行良好。
我的问题:
有了
Push Segue
,我不需要添加 BackButton (NavigationItem
) 来返回 View_A,因为有一个“NavigationItem”由系统自动装箱。我尝试了其他类型的转场,例如Modal
、Popover
,并且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 ,如下所示:
我使用的另一种技术是,创建单个 NavigationController 类,并设置所有需要的属性(颜色、字体等),然后将 Storyboard 中的所有 NavigationController 链接到该 NavigationController 类。
有了它,您就不必重新配置每个 NavigationController。
关于ios - 无法设置 NavigationItem 的 TintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29959821/