swift中UINavigationController的使用

// 导航视图控制器标题
self.navigationItem.title = "navigationController"
        
// 导航视图控制器样式
self.navigationController!.setNavigationStyleDefault()
        
// 导航视图控制器左按钮
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "present",style: UIBarButtonItemStyle.Done,target: self,action: Selector("presentClick"))
self.navigationItem.leftBarButtonItem!.tintColor = UIColor.greenColor()
        
// 导航视图控制器右按钮
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "push",action: Selector("pushClick"))
self.navigationItem.rightBarButtonItem!.tintColor = UIColor.orangeColor()
// 注意:如果下个视图控制器的导航栏样式与当前的不一样时,返回当前视图控制器时,需要重置下样式
override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 导航视图控制器样式
        self.navigationController!.setNavigationStyleDefault()
}
// 导航视图控制器样式
self.navigationController!.setNavigationStyle(UIColor.orangeColor(),textFont: UIFont.boldSystemFontOfSize(12.0),textColor: UIColor.yellowColor())

// 导航栏隐藏,或显示
let button = UIButton(frame: CGRectMake(10.0,10.0,(CGRectGetWidth(self.view.frame) - 10.0 * 2),40.0))
self.view.addSubview(button)
button.backgroundColor = UIColor.lightGrayColor()
button.setTitle("隐藏导航栏",forState: UIControlState.normal)
button.setTitleColor(UIColor.blackColor(),forState: UIControlState.normal)
button.setTitleColor(UIColor.redColor(),forState: UIControlState.Highlighted)
button.addTarget(self,action: Selector("hiddenClick:"),forControlEvents: UIControlEvents.TouchUpInside)
button.selected = false
func hiddenClick(button:UIButton)
{
        button.selected = !button.selected
        
        let isSelected = button.selected
        let text = (isSelected ? "隐藏" : "显示")
        print("\(text) 导航栏")
        
        if isSelected
        {
            button.setTitle("显示导航栏",forState: UIControlState.normal)
            self.navigationController!.setNavigationBarHidden(true,animated: true)
        }
        else
        {
            button.setTitle("隐藏导航栏",forState: UIControlState.normal)
            self.navigationController!.setNavigationBarHidden(false,animated: true)
        }
        
}
// 导航栏样式设置方法
// MARK: - 导航栏样式设置
/// 设置认导航栏样式
func setNavigationStyleDefault()
{
        self.setNavigationStyle(UIColor.whiteColor(),textFont: UIFont.boldSystemFontOfSize(18.0),textColor: UIColor.blackColor())
}
    
/// 导航栏样式设置(自定义背景颜色、字体)
func setNavigationStyle(backgroundColor:UIColor,textFont:UIFont,textColor:UIColor)
{
        if self.navigationBar.respondsToSelector(Selector("barTintColor"))
        {
            // 背景色
            self.navigationBar.barTintColor = backgroundColor
            self.navigationBar.translucent = false
            self.navigationBar.tintColor = UIColor.whiteColor()
            
            // 字体
            self.navigationBar.titleTextAttributes = [NSFontAttributeName:textFont,NSForegroundColorAttributeName:textColor];
            
            // 导航底部1px的阴影颜色-修改
            /*
            self.navigationBar.shadowImage = UIImage(named: "")
            [self.navigationBar setShadowImage:kImageWithColor(kColorSeparatorline)];
            */
            
            // 导航底部1px的阴影-遮住
            let maskLayer = CAShapeLayer.init()
            maskLayer.backgroundColor = UIColor.redColor().CGColor;
            let maskRect = CGRectMake(0,-20.0,CGRectGetWidth(self.navigationBar.frame),(20.0 + CGRectGetHeight(self.navigationBar.frame)));
            maskLayer.path = CGPathCreateWithRect(maskRect,nil);
            self.navigationBar.layer.mask = maskLayer;
        }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...