iOS 导航栏定制


工具分类

extension UINavigationBar {

    

    func initWithViewController(control:UIViewController,title:String) {

        control.title=title;

        self.barTintColor = UIColor(rgba: navigationBarColor);

// 定义导航栏标题栏字体属性

        self.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(), NSFontAttributeName:UIFont.boldSystemFontOfSize(17)];

        // 定义导航栏的系统按钮的颜色属性

        self.tintColor = UIColor.whiteColor();

        UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false);

    }

    

    func initWithLeftAction(control:UIViewController, imgNormal:String, imgHighlighted:String, action:Selector) {

        

        initWithLeftAction(control, imgNormal: imgNormal, imgHighlighted: imgHighlighted, imgSelected: "", action: action);

    }

    

    func initWithLeftAction(control:UIViewController, imgNormal:String, imgHighlighted:String, imgSelected:String,action:Selector) {

        

        let leftButton:UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30));

        leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

        leftButton.setImage(UIImage(named: imgNormal), forState: .Normal);

        leftButton.setImage(UIImage(named: imgHighlighted), forState: .Highlighted);

        leftButton.setImage(UIImage(named: imgSelected), forState: .Selected);

        

        leftButton.addTarget(control, action: action, forControlEvents: .TouchUpInside);

        let leftBarButton:UIBarButtonItem = UIBarButtonItem(customView: leftButton);

        control.navigationItem.leftBarButtonItem = leftBarButton;

    }

    

    func initWithRightAction(control:UIViewController, imgNormal:String, imgHighlighted:String, action:Selector) {

        

        let rightButton:UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30));

        rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

        rightButton.setImage(UIImage(named: imgNormal), forState: .Normal);

        rightButton.setImage(UIImage(named: imgHighlighted), forState: .Highlighted);

        

        rightButton.addTarget(control, action: action, forControlEvents: .TouchUpInside);

        let rightBarButton:UIBarButtonItem = UIBarButtonItem(customView: rightButton);

        control.navigationItem.rightBarButtonItem = rightBarButton;

    }

    

}


// 这里要注意的是返回按钮必须在上一级控制器中定制

返回按钮的字体颜色由下一级控制的navigationBar.tingColor决定

extension UINavigationController {

    

    func pushViewController(controller: UIViewController, animated: Bool, topController: UIViewController, isCustom: Bool) {

        

        pushViewController(controller, animated: animated, topController: topController, isCustom: isCustom, title: "");

    }

    

    func pushViewController(controller: UIViewController, animated: Bool, topController: UIViewController, isCustom: Bool, title:String) {

        

        if isCustom == false {

            pushViewController(controller, animated: animated);

            return;

        }

        

        var backButtonImage = UIImage(named: "bt_back")!;

        backButtonImage = backButtonImage.resizableImageWithCapInsets(UIEdgeInsetsMake(0, backButtonImage.size.width - 1, 0, 0));

        let backItem = UIBarButtonItem(title: title, style: .Plain, target: nil, action: nil);


        UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(6, 0), forBarMetrics: .Default);

        // 设置返回按钮的文字不可见

        UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default);

        

        topController.navigationItem.backBarButtonItem = backItem;

        

        pushViewController(controller, animated: animated);

    }

    

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值