加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中UINavigationController的使用

发布时间:2020-12-14 06:40:23 所属栏目:百科 来源:网络整理
导读:// 导航视图控制器标题self.navigationItem.title = "navigationController" // 导航视图控制器样式self.navigationController!.setNavigationStyleDefault() // 导航视图控制器左按钮self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "pres
// 导航视图控制器标题
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;
        }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读