如何设置UINavigationBar背景怎么换
展开全部
利用objective-c的Category语法 扩展UINavigationBar 类
具体代码为
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: @"navbar_background.png"];
CGPoint point = {0,0};
[img drawAtPoint:point];
}
@end
////////////////////方法2////////////////////////////////////////////////////
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
//加入旋转坐标系代码
// Drawing code
UIImage *navBarImage = [UIImage imageNamed:@"LOGO_320×44.png"];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGPoint center=self.center;
CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));
CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height), cgImage);
CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height), navBarImage.CGImage);
CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height), cgImage);
}
@end
old code
CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width,self.frame.size.height), navBarImage.CGImage);
方法三:
有了这个,你还会扩展drawRect自定义导航栏背景吗?
查了很多资料,网上的自定义导航栏的方法,清一色的是扩展navigationBar的drawRect方法.然而这样的扩展会影响到工程里所有的navigationBar.或许你并不想这么做,而且很多不合常规的UI用这种方法根本没法实现.
做了无数实验,尝试了各种方法,今天跟大家分享一个新方法:
#import <QuartzCore/QuartzCore.h>
@interface DDNavigationViewController : UINavigationController<UINavigationControllerDelegate> {
CALayer *_barBackLayer;
}
@end
@implementation DDNavigationViewController
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
self.delegate = self;
return self;
}
- (void)loadView {
[super loadView];
UINavigationBar *bar = self.navigationBar;
CALayer*layer = [CALayer layer];
UIImage *navBarImage = [UIImage imageNamed:@"navigationBarBackground.png"];
layer.contents = (id)navBarImage.CGImage;
layer.frame= CGRectMake(0, 0, 320, navBarImage.size.height);
[bar.layer insertSublayer:layer atIndex:0];
_barBackLayer = layer;
}
#pragma mark -
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[_barBackLayer removeFromSuperlayer];
[navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];
}
@end
———————————————分割线——————————————————
满意请采纳(⊙o⊙),以上是软硬谦施团队为你带来的回答。
如果有什么不懂的可以追问,以后有什么问题可以向我提问或者向我的团队提问。(*^__^*) 嘻嘻……
具体代码为
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: @"navbar_background.png"];
CGPoint point = {0,0};
[img drawAtPoint:point];
}
@end
////////////////////方法2////////////////////////////////////////////////////
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
//加入旋转坐标系代码
// Drawing code
UIImage *navBarImage = [UIImage imageNamed:@"LOGO_320×44.png"];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGPoint center=self.center;
CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));
CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height), cgImage);
CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height), navBarImage.CGImage);
CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height), cgImage);
}
@end
old code
CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width,self.frame.size.height), navBarImage.CGImage);
方法三:
有了这个,你还会扩展drawRect自定义导航栏背景吗?
查了很多资料,网上的自定义导航栏的方法,清一色的是扩展navigationBar的drawRect方法.然而这样的扩展会影响到工程里所有的navigationBar.或许你并不想这么做,而且很多不合常规的UI用这种方法根本没法实现.
做了无数实验,尝试了各种方法,今天跟大家分享一个新方法:
#import <QuartzCore/QuartzCore.h>
@interface DDNavigationViewController : UINavigationController<UINavigationControllerDelegate> {
CALayer *_barBackLayer;
}
@end
@implementation DDNavigationViewController
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
self.delegate = self;
return self;
}
- (void)loadView {
[super loadView];
UINavigationBar *bar = self.navigationBar;
CALayer*layer = [CALayer layer];
UIImage *navBarImage = [UIImage imageNamed:@"navigationBarBackground.png"];
layer.contents = (id)navBarImage.CGImage;
layer.frame= CGRectMake(0, 0, 320, navBarImage.size.height);
[bar.layer insertSublayer:layer atIndex:0];
_barBackLayer = layer;
}
#pragma mark -
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[_barBackLayer removeFromSuperlayer];
[navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];
}
@end
———————————————分割线——————————————————
满意请采纳(⊙o⊙),以上是软硬谦施团队为你带来的回答。
如果有什么不懂的可以追问,以后有什么问题可以向我提问或者向我的团队提问。(*^__^*) 嘻嘻……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询