iOS 导航栏定制
2016-01-08 15:00
309 查看
工具分类
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);
}
}
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);
}
}
相关文章推荐
- iOS Newbie - Xcode 7 & iOS 9 页面间传值
- iOS 引入字体库
- iOS 真机调试报错汇总
- IOS界面全屏模式
- iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
- IOS下使用AVFoundation实现条形码和二维码扫描
- iOS 截屏/将图片存储到相册或沙盒目录下
- iOS中打印日志显示系统详细时间,类名,行号及打印值
- iOS动态获取键盘高度实现流畅的键盘输入框开发
- IOS调试lldb命令常用,po, Xcode控制台调试
- iOS-NSString汉子转拼音
- Xcode描述文件路径
- iOS获取时间戳,字典转换成json数据
- iOS 随机产生32位字符串
- ios手势识别代理
- iOS: 如何在工程中设置 DEBUG 模式?
- ionic-v1.7.12-ios版修改
- iOS视图层次结构
- iosURL字符串的URLencode 和 encodeURIComponent(base64) 去除get时因特殊字符至错
- IOS的处理touch事件处理(按照手指的移动移动一个圆,开发环境用的ios7,storyboard)