1、改变高度
自定义UINavigationBar的新类别:
[cpp] view
plaincopy
- //UINavigationBar+BackgoundImage.h
- #import <Foundation/Foundation.h>
- @interface UINavigationBar (BackgoundImage)
- @end
在新类别的实现中,覆盖原有类的方法 - (void)drawRect:(CGRect)rect :
[cpp] view
plaincopy
- //UINavigationBar+BackgoundImage.m
- #import "UINavigationBar+BackgoundImage.h"
- @implementation UINavigationBar (BackgoundImage)
- - (void)layoutSubviews {
- CGRect barFrame = self.frame;
- barFrame.size.height = 100.0; //新的高度
- self.frame = barFrame;
- }
- //其实只需要覆盖该方法就行,把self.frame.size.height改成100.0就OK
- - (void)drawRect:(CGRect)rect {
- UIImage *image = [UIImage imageNamed:@"Logo.PNG"];
- [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
- }
- @end
至此可以发现工程中的NavigationBar的高度已经变化。
2、接下来改变背景图片,(参考http://stackoverflow.com/questions/8375092/xcode4-2-storyboard-navigation-controller-how-to-set-custom-image-for-uinavig):
在XXXAppDalegate.m中的委托方法:
[cpp] view
plaincopy
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //......
- return YES;
- }
的"return YES"前加入代码:
[cpp] view
plaincopy
- [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Logo.PNG"] forBarMetrics:UIBarMetricsDefault]; //this adds the image
成功。
在第1步中的高度可以直接取图片的高度,这样比较方便。
时间: 2024-10-13 04:08:58