【发布时间】:2012-06-19 22:10:33
【问题描述】:
我希望 NavigationBar 的行为相同,但希望更改它的外观。我在网上找到了很多方法,但我不确定哪一种是 iOS 5.0 的最佳结果。导航栏将如下所示:
【问题讨论】:
标签: iphone ios uinavigationcontroller
我希望 NavigationBar 的行为相同,但希望更改它的外观。我在网上找到了很多方法,但我不确定哪一种是 iOS 5.0 的最佳结果。导航栏将如下所示:
【问题讨论】:
标签: iphone ios uinavigationcontroller
由于您的目标是 iOS 5,我肯定会使用 Appearance proxy 选择 customizing UINavigationBar。然后,您可以轻松设置自己的图像,它们将应用于您应用程序中的所有导航栏,无需子类化。
您还可以通过customizing UIBarButtonItem自定义导航栏中的按钮。返回按钮有backButtonBackgroundImageForState:barMetrics:,其他按钮有backgroundImageForState:barMetrics:。
【讨论】:
我也一直在寻找这个东西,也没有找到一个简单的解决方案!感谢我的一位朋友和示例代码,我们使用自定义导航栏类制作了它,该类可以导入到任何视图控制器类中。
.h 文件:
#import <UIKit/UIKit.h>
@interface NATitleBar : UIView {
NSInteger tag;
}
@property ( nonatomic) IBOutlet UIImageView *imageView;
@property ( nonatomic) IBOutlet UILabel *label;
@property ( nonatomic) IBOutlet UIButton *back;
@property ( nonatomic) IBOutlet UIButton *home;
/**
* Supports UIButton-style adding targets
*/
@end
.m 文件:
#import "NATitleBar.h"
@implementation NATitleBar
@synthesize imageView;
@synthesize label;
@synthesize back;
@synthesize home;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"NATitleBar" owner:self options:nil];
[self addSubview:[views objectAtIndex:0]];
// customize the view a bit
//self.imageView.layer.borderWidth = 1.0;
//self.imageView.layer.borderColor = [UIColor colorWithWhite:0.4 alpha:0.4].CGColor;
//self.imageView.clipsToBounds = YES;
//self.imageView.layer.cornerRadius = 5.0;
}
return self;
}
#pragma mark - Overriden Setters / Getters
- (void)setTag:(NSInteger)aTag {
self.back.tag = aTag;
}
- (NSInteger)tag {
return self.back.tag;
}
@end
然后对于 Nib 文件,我们有以下内容:
您可以在 Nib 文件中添加或删除图像,以根据需要制作 GUI。
现在您必须将该类导入您希望使用自定义导航控制器拥有的任何视图控制器,并定义两种方法(或一种,如果您不想要“主页”按钮。在.h:
- (void) back;
在.m:
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
【讨论】: