swift中UINavigationController的使用

前端之家收集整理的这篇文章主要介绍了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;
        }
}

原文链接:https://www.f2er.com/swift/322816.html

猜你在找的Swift相关文章