我想創建UINavigationBar
具有多項目的自定義子類。但我不知道這件事。如何創建自定義NavigationBar子類並在其中添加多個項目
也我想要得到這個自定義NavigationBar
在UIViewControllers
並改變它。
請指導我: 1-如何創建自定義NavigationBar
子類的項目。 2-如何獲取此自定義導航並更改其中的項目。
這是我的代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
ViewController *rootView = [[ViewController alloc]init];
UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[NavBar class] toolbarClass:nil];
navi.viewControllers = @[rootView];
self.window.rootViewController = navi;
[window makeKeyAndVisible];
return YES;
}
NavBar.m
#import "NavBar.h"
@implementation NavBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
[self setupBackground];
}
- (void)setupBackground {
self.backgroundColor = [UIColor clearColor];
self.tintColor = [UIColor blueColor];
// make navigation bar overlap the content
self.translucent = YES;
self.opaque = NO;
// remove the default background image by replacing it with a clear image
[self setBackgroundImage:[self.class maskedImage] forBarMetrics:UIBarMetricsDefault];
// remove defualt bottom shadow
[self setShadowImage: [UIImage new]];
}
+ (UIImage *)maskedImage {
const CGFloat colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [UIImage imageNamed:@"nav-white-pixel-bg.jpg"];
return [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
什麼? – Fawkes
也是如此,雖然我經常發現這對自定義間距等更好...這並沒有被定義爲一個要求,但我會將它添加到我的答案。乾杯。 – Magoo