。h
#import <UIKit/UIKit.h>
@interface UINavigationBar (UINavigationBarCategory)
UIImageView *backgroundView;
- (void)setBackgroundImage:(UIImage*)image;
- (void)insertSubview:(UIView *)viewatIndex:(NSInteger)index;
@end
。m
#import "CustomNavigationBar.h"
@implementation UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image
{
if(image ==nil)
{
[backgroundView removeFromSuperview];
}
else
{
backgroundView = [[UIImageView alloc]initWithImage:image];
backgroundView.frame = CGRectMake(0.f, 0.f,self.frame.size.width, self.frame.size.height);
backgroundView.autoresizingMask = UIViewAutoresizingFlexib leWidth |UIViewAutoresizingFlexib leHeight;
[self addSubview:backgroundView];
[self sendSubviewToBack:backgroundView];
[backgroundView release];
}
}
//for other views
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
[superinsertSubview:view atIndex:index];
[selfsendSubviewToBack:backgroundView];
}
@end
#import <UIKit/UIKit.h>
@interface UINavigationBar (UINavigationBarCategory)
UIImageView *backgroundView;
- (void)setBackgroundImage:(UIImage*)image;
- (void)insertSubview:(UIView *)viewatIndex:(NSInteger)index;
@end
。m
#import "CustomNavigationBar.h"
@implementation UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image
{
}
//for other views
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
}
@end
本文介绍如何通过Objective-C自定义UINavigationBar的背景图片,并确保其能够适配不同的屏幕尺寸,同时保持良好的视觉效果。此外,还提供了一个插入视图的方法以确保背景图始终处于最底层。
1296

被折叠的 条评论
为什么被折叠?



