【发布时间】:2016-09-03 16:28:27
【问题描述】:
我将在不同视图控制器中的所有导航控制器上使用类似的属性,但一些导航项和标题除外。
我做了一个扩展计划,以便能够调用它并设置我的默认值;但是,它什么也没做。当我只是将它放在实际类中时,代码确实有效,但在我调用 SetDefaults 时无效。
扩展:
extension UINavigationController {
func setDefaults(titleText: String){
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
//Set title label
let label = UILabel()
label.font = UIFont(name: appDel.regularDefaultFont, size: 16)
label.text = titleText
label.kern(1.0)
label.sizeToFit()
self.navigationItem.titleView = label
//Set white background tint color
self.navigationBar.barTintColor = UIColor.whiteColor()
//Set navigation bar bottom line
let bottomBorderRect = CGRect(x: 0, y: UIScreen.mainScreen().bounds.height, width: UIScreen.mainScreen().bounds.width, height: 1)
let bottomBorderView = UIView(frame: bottomBorderRect)
bottomBorderView.backgroundColor = UIColor(r: 250, g: 250, b: 250)
self.navigationBar.addSubview(bottomBorderView)
}
}
使用导航控制器从类调用 SetDefaults:
self.navigationController.SetDefaults("Login")
【问题讨论】:
-
你能在你的扩展中设置一个断点,看看是否到达了吗?
-
确保您实际上是在右侧导航控制器上调用
setDefaults。如果你使用 UISplitViewController 视图控制器自己的导航控制器属性可能不是屏幕上的那个 -
kabiroberai,我试过了,它确实到达了那里。 @Luke 我在刚刚展示的视图控制器中调用 setDefaults 。它是否正确?查看我对以下回复的评论,查看我用于显示视图控制器的代码。
标签: ios swift uinavigationcontroller