工具分类
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);
}
}