8

Tôi muốn phân lớp UINavigationBar (để đặt hình nền tùy chỉnh & màu văn bản) và sử dụng cho tất cả thanh điều hướng trong ứng dụng của tôi. Nhìn vào các tài liệu API cho UINavigationController, nó trông giống như Navigationbar là readonly:Tạo lớp con UINavigationBar ... làm cách nào để sử dụng nó trong UINavigationController?

@property (nonatomic, chỉ đọc) UINavigationBar * Navigationbar

Có cách nào để thực sự sử dụng một UINavigationBar tùy chỉnh trong UIViewControllers của tôi? Tôi biết rằng các ứng dụng khác đã thực hiện thanh menu tùy chỉnh, như flickr:

http://img.skitch.com/20100520-bpxjaca11h5xne5yakjdc2m6xx.png

Đây là lớp con UINavigationBar tôi:

#import <UIKit/UIKit.h> 

@interface MyNavigationBar : UINavigationBar <UINavigationBarDelegate> { 

} 

@end 

thi

#import "MyNavigationBar.h" 

@implementation MyNavigationBar 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    // override the standard background with our own custom one 
    UIImage *image = [[UIImage imageNamed:@"navigation_bar_bgd.png"] retain]; 
    [image drawInRect:rect]; 
    [image release]; 
} 

#pragma mark - 
#pragma mark UINavigationDelegate Methods 

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ 
    // use the title of the passed in view controller 
    NSString *title = [viewController title]; 

    // create our own UILabel with custom color, text, etc 
    UILabel *titleView = [[UILabel alloc] init]; 
    [titleView setFont:[UIFont boldSystemFontOfSize:18]]; 
    [titleView setTextColor:[UIColor blackColor]]; 
    titleView.text = title; 
    titleView.backgroundColor = [UIColor clearColor]; 
    [titleView sizeToFit]; 

    viewController.navigationItem.titleView = titleView; 
    [titleView release]; 

    viewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.1 green:0.2 blue:0.3 alpha:0.8]; 
} 
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

tôi biết rằng tôi có thể sử dụng danh mục để thay đổi nền hình ảnh, nhưng tôi vẫn muốn có thể đặt màu văn bản của tiêu đề thanh điều hướng

@implementation UINavigationBar (CustomImage) 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed: @"navigation_bar_bgd.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

bất kỳ đề xuất hoặc giải pháp nào khác? Về cơ bản, tôi muốn tạo nền sáng và văn bản tối màu như thanh điều hướng ứng dụng của Flickr

Trả lời

2

Đặt thuộc tính "màu" của UINavigationBar thành màu bạn muốn.

+0

Điều này sẽ làm việc để thay đổi nền của thanh điều hướng, nhưng tôi cũng muốn thay đổi màu văn bản mặc định của thanh điều hướng, vì tôi muốn sử dụng màu nền có màu sáng, ví dụ: nếu tôi muốn sử dụng nền trắng, tôi cần phải có thể thay đổi văn bản màu trắng mặc định thành thứ gì đó tối – funkadelic

2

Thử tạo [UIColor colorWithPatternImage:(UIImage*)image] và đặt nó vào thuộc tính backgroundColor của NavBar. Tôi chưa thử điều này, nhưng tôi sẽ làm ngay bây giờ.

4

Tôi không biết nếu bạn đã nhận ra điều này. Nhưng đối với những người khác mà có thể có vấn đề này ...

Những gì bạn cần làm là để xác định các lớp trong XIB của bạn để tên lớp của bạn (trong trường hợp này MyNavigationBar.

Kiểm tra phiên 123 trong WWDC 2011.

Các vấn đề liên quan