#pragma mark 仿iOS7ButtonItem - (id)initWithTitle:(NSString *)title addTarget:(id)target action:(SEL)action {
if (self = [super init]){
// 创建一个普通按钮并设置按钮样式 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:title forState:UIControlStateNormal]; [button setTitleColor:kBlueColor forState:UIControlStateNormal]; [button setTitleColor:kBlueColorSelected forState:UIControlStateHighlighted]; [button setTitleColor:kBlueColorDisable forState:UIControlStateDisabled]; button.titleLabel.font = [UIFont boldSystemFontOfSize:18]; CGSize size = [button.titleLabel.text sizeWithFont:button.titleLabel.font]; button.bounds = (CGRect){CGPointZero, size.width + 10, size.height};
button.contentHorizontalAlignment = iOS7 ? UIControlContentHorizontalAlignmentRight : UIControlContentHorizontalAlignmentLeft; // 设置按钮事件处理 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
// 将item初始化为上述按钮样式 self = [[UIBarButtonItem alloc] initWithCustomView:button]; } return self; }
#pragma mark 仿iOS7返回按钮 - (id)initWithBackButtonTitle:(NSString *)title addTarget:(id)target action:(SEL)action { if (self = [super init]){ // 创建一个普通按钮并设置按钮样式 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 蓝色返回箭头 UIImage *normal = [UIImage resizeImage:@"navigation_button_back.png"]; [button setImage:normal forState:UIControlStateNormal];
// 按钮文字及属性 [button setTitle:title forState:UIControlStateNormal]; [button setTitleColor:kBlueColor forState:UIControlStateNormal]; [button setTitleColor:kBlueColorSelected forState:UIControlStateHighlighted];
// 按钮布局 button.imageEdgeInsets = UIEdgeInsetsMake(0, iOS7 ? -15 : 4, 0, 0); button.titleEdgeInsets = UIEdgeInsetsMake(0, iOS7 ? -10 : 6, 0, 0);
CGFloat width = [button.titleLabel.text sizeWithFont:button.titleLabel.font].width + 20; button.bounds = (CGRect){CGPointZero, width, normal.size.height};
// 设置按钮事件处理 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
// 将item初始化为上述按钮样式 self = [[UIBarButtonItem alloc] initWithCustomView:button]; } return self; }
|