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", style: UIBarButtonItemStyle.Done, target: self, 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://github.com/potato512/SYSwiftLearning






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值