对Button进行一次封装
- (UIButton *)buttonWithFrame:(CGRect)frame withNormalTitle:(NSString *)title withOtherStateTitle:(NSString *)otherTitle action:(SEL)action { UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; UIImage *disabledButtonBackgroundImage = [[UIImage imageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = frame; [button setTitle:title forState:UIControlStateNormal]; [button setTitle:otherTitle forState:UIControlStateDisabled]; [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal]; [button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; return button; }
调用:
testButton = [self buttonWithFrame:CGRectMake(100,100,200,100) withNormalTitle:@"测试" withOtherStateTitle:@"测试" action:@selector(TestAction)];
测试效果:
本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208677,如需转载请自行联系原作者