ios UIButton改背景
以下orangeButton.png与orangeButtonHighlight.png分别用于按钮平常状态和被点击时的状态:
UIImage *buttonImage = [[UIImage imageNamed:@"orangeButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)]; UIImage *buttonImageHighlight = [[UIImage imageNamed:@"orangeButtonHighlight.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)]; // Set the background for any states you plan to use [saveButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; [saveButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
代码创建一个UIButton:
UIButton *btnClear = [[UIButton alloc] init]; btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; btnClear.frame = CGRectMake(115, 200, 90, 40); [btnClear setTitle:@"Clear" forState:UIControlStateNormal]; [btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal]; [btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnClear];