自定义导航栏按钮
首先,写一个类,继承于UIButton。
?
头文件:
?
#import <Foundation/Foundation.h>@interface BlueButton : UIButton {}- (id)init;@end
?
实现文件:
?
#import "BlueButton.h"@implementation BlueButton- (id)init { if(self = [super init]) { self.frame = CGRectMake(0, 0, 49.0, 30.0); self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; UIImage *image = [UIImage imageNamed:@"blueButton.jpg"]; UIImage *stretchImage = [image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0]; [self setBackgroundImage:stretchImage forState:UIControlStateNormal]; self.backgroundColor = [UIColor clearColor]; [self setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; self.titleShadowOffset = CGSizeMake(0, -1); self.titleLabel.font = [UIFont boldSystemFontOfSize:13]; } return self;}@end
?
示例:
?
- (void)viewDidLoad {[super viewDidLoad];BlueButton *blueButton = [[BlueButton alloc] init];[blueButton setTitle:@"Add" forState:UIControlStateNormal];[blueButton addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:blueButton];self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(Cancel)];self.navigationItem.rightBarButtonItem = button;[button release];[blueButton release];}
?
示例图: