您的位置:首页 > 其它

自定义 NavigationBar

2016-03-21 22:46 411 查看
话说自古武林剑法门派繁多,所以就有了每年9月9日的华山论剑。。。

iOS开发某些方面也是如此。拿自定义UINavigationBar这个很小的方面,也有N种方法,导致我在找寻答案的过程中走了很多弯路,多花了不少时间。现在就将这些方法做一下对比,谁优谁劣,留给读者思考:

1.辟邪剑法

此剑法非常初级

只能实现系统定义的样式。如下:

[代码]c#/cpp/oc代码:

1
[self.navigationController.navigationBar
setBarStyle:UIBarStyleBlackOpaque];
2.松风剑法

此剑法中规中矩,能有不同变化。

可以随意改变bar的颜色,如下:

[代码]c#/cpp/oc代码:

1
[self.navigationController.navigationBar
setTintColor:[UIColororangeColor]];
3.太极剑法

此剑法看似神奇,其实不厉害。

只能修改titleView,不能用于自定义背景,如下:

[代码]c#/cpp/oc代码:

1
UIImage
*logo=[UIImageimageNamed:
@"title_bg"
];
2
[self.navigationItem
setTitleView:[[[UIImageViewalloc]initWithImage:logo]autorelease]];
如果你想用它修改背景一定会很失望的。。。

4.七星剑法

此剑法朴树迷离,闪瞎你dog眼,不过有种种限制,特定条件才能发挥威力。

只对iOS5有效。如下:

[代码]c#/cpp/oc代码:

01
-
(
void
)customizeAppearance
02
{
03
//
Createresizableimages
04
UIImage
*gradientImage44=[[UIImageimageNamed:
@"surf_gradient_textured_44"
]
05
resizableImageWithCapInsets:UIEdgeInsetsMake(0,
0,0,0)];
06
UIImage
*gradientImage32=[[UIImageimageNamed:
@"surf_gradient_textured_32"
]
07
resizableImageWithCapInsets:UIEdgeInsetsMake(0,
0,0,0)];
08
09
//
Setthebackgroundimagefor*all*UINavigationBars
10
[[UINavigationBar
appearance]setBackgroundImage:gradientImage44
11
forBarMetrics:UIBarMetricsDefault];
12
[[UINavigationBar
appearance]setBackgroundImage:gradientImage32
13
forBarMetrics:UIBarMetricsLandscapePhone];
14
15
//
Customizethetitletextfor*all*UINavigationBars
16
[[UINavigationBar
appearance]setTitleTextAttributes:
17
[NSDictionary
dictionaryWithObjectsAndKeys:
18
[UIColor
colorWithRed:255.0/255.0green:255.0/255.0blue:255.0/255.0alpha:1.0],
19
UITextAttributeTextColor,
20
[UIColor
colorWithRed:0.0green:0.0blue:0.0alpha:0.8],
21
UITextAttributeTextShadowColor,
22
[NSValue
valueWithUIOffset:UIOffsetMake(0,-1)],
23
UITextAttributeTextShadowOffset,
24
[UIFont
fontWithName:
@"Arial-Bold"
size:0.0],
25
UITextAttributeFont,
26
nil]];
27
28
}<br>
简单一点,就是下面的代码:

[代码]c#/cpp/oc代码:

1
UINavigationBar
*navBar=[myNavControllernavigationBar];
2
if
([navBar
respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
3
{
4
//
setglobabllyforallUINavBars
5
[[UINavigationBar
appearance]setBackgroundImage:[UIImageimageNamed:
@"brnlthr_nav.jpg"
]
forBarMetrics:UIBarMetricsDefault];
6
  
//
...
7
}
5.太极剑法

此剑法极简,主要靠内力,但是同样受到限制。

仅对iOS5之前版本有效,如下:

[代码]c#/cpp/oc代码:

1
@implementation
UINavigationBar(CustomImage)
2
-
(
void
)drawRect:(CGRect)rect
{
3
//
Drawingcode
4
UIImage
*image=[[UIImageimageNamed:
@"header.png"
]
retain];
5
[image
drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
6
[image
release];
7
}
8
@end
6.雌雄双股剑法

将上述七星剑法和太极剑法接合,可形成雌雄双股剑法,根据iOS版本不同发挥各自威力,从而不受限制。

7.两仪剑法

此剑法源于太极剑法,所谓“太极生两仪”。。。套路略有不同,不过还是仅限于iOS5之前版本:

[代码]c#/cpp/oc代码:

1
@
interface
MyNavigationBar
:UINavigationBar
2
3
@end

[代码]c#/cpp/oc代码:

01
@implementation
MyNavigationBar
02
03
-
(
void
)drawRect:(CGRect)rect
04
{
05
[super
drawRect:rect];
06
}
07
@end
08
09
@implementation
UINavigationBar(LazyNavigationBar)
10
+
(Class)
class
{
11
return
NSClassFromString(
@"MyNavigationBar"
);
12
}
13
14
-(
void
)drawRect:(CGRect)rect
{
15
UIImage
*backImage=[UIImageimageNamed:
@"title_bg"
];
16
[backImage
drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
17
}
18
@end
8.金蛇剑法

此剑法劲道威猛,难以招架。

首先写CustomNaviBar类:

[代码]c#/cpp/oc代码:

1
@
interface
CustomNaviBar
:UINavigationBar
2
@end
3
4
@implementation
CustomNaviBar
5
-
(
void
)drawRect:(CGRect)rect
{
6
[[UIImage
imageNamed:
@"title_bg"
]
drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
7
}
8
@end
然后创建xib文件,拖一个UINavigationController出来,将UINavigationController中的UINavigationBar替换为自定义的CustomNaviBar

然后,使用xib文件来创建UINavigationController:

[代码]c#/cpp/oc代码:

01
-
(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
02
{
03
self.window
=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
04
05
UINib
*nib=[UINibnibWithNibName:
@"Empty"
bundle:nil];
06
UINavigationController*
nc=[[nibinstantiateWithOwner:niloptions:nil]objectAtIndex:0];
//
07
self.window.rootViewController
=nc;
08
09
self.viewController
=nil;
10
11
[self.window
makeKeyAndVisible];
12
return
YES;
13
}
此方法通吃所有iOS平台,有独步武林盟主的实力。。

9.独孤九剑

传说中的终极剑法,威力无比。

适用所有IOS版本。

首先,为appDelegate增加一个navigationController属性:

[代码]c#/cpp/oc代码:

1
@
interface
DymAppDelegate
:UIResponder<UIApplicationDelegate>
2
{
3
UINavigationController
*navController_;
4
}
5
@property
(strong,nonatomic)UIWindow*window;
6
@property
(strong,nonatomic)DymViewController*viewController;
7
8
@property
(nonatomic,
readonly
,
retain)UINavigationController*navigationController;
9
@end
然后,将此设为rootViewController:

[代码]c#/cpp/oc代码:

01
-
(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
02
{
03
self.window
=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
04
05
self.viewController
=[[[DymViewControlleralloc]initWithNibName:
@"DymViewController"
bundle:nil]
autorelease];
06
07
self.window.rootViewController
=self.navigationController;
08
09
self.viewController
=nil;
10
11
[self.window
makeKeyAndVisible];
12
return
YES;
13
}
下面是此剑法的心法:

[代码]c#/cpp/oc代码:

01
-
(UINavigationController*)navigationController{
02
if
(navController_
==nil){
03
UINavigationController
*navController=[[UINavigationControlleralloc]initWithRootViewController:self.viewController];
04
05
//
Archivenavigationcontrollerforchangingnavigationbarclass
06
[navController
navigationBar];
07
NSMutableData
*data=[[NSMutableDataalloc]init];
08
NSKeyedArchiver
*archiver=[[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
09
[archiver
encodeObject:navControllerforKey:kRootKey];
10
[archiver
finishEncoding];
11
[archiver
release];
12
[navController
release];
13
14
//
Unarchiveitwithchangingnavigationbarclass
15
NSKeyedUnarchiver
*unarchiver=[[NSKeyedUnarchiveralloc]initForReadingWithData:data];
16
[unarchiver
setClass:[CustomNaviBar
class
]
17
forClassName:NSStringFromClass([UINavigationBar
class
])];
18
navController_
=[[unarchiverdecodeObjectForKey:kRootKey]retain];
19
[unarchiver
release];
20
21
[data
release];
22
}
23
return
navController_;
24
}
利用NSKeyedArchiver和NSKeyedUnarchiver来修改navigationbar。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: