在應用UIBUtton的時候我們有時需要同時設定圖片和文字,下面代碼僅供參考:
UIButton *_backButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[_backButton setFrame:CGRectMake(12,
8, 64, 28)];
[_backButton setTitle:@"返回"
forState:UIControlStateNormal]; //設定button在沒有選中的時候顯示的字型
_backButton.titleLabel.font = [UIFont
systemFontOfSize:14.0f]; //設定button顯示字型的大小
[_backButton setBackgroundImage:[UIImage
imageNamed:@"backButton.png"]
forState:UIControlStateNormal]; //設定button背景顯示圖片
[self.view addSubview:_backButton];
出了上面的操作意外,我們還可以同時設定button在選中(按下之後)以及正常狀態下(沒有選中)顯示文字和圖片的不同,
UIButton *_backButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[_backButton setFrame:CGRectMake(12,
8, 64, 28)];
[_backButton setTitle:@"返回"
forState:UIControlStateNormal];
[_backButton setTitle:@"Down" forState:UIControlStateHighlighted];
[_backButton setBackgroundColor:[UIColor clearColor]];
[_backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_backButton.titleLabel.font = [UIFont
systemFontOfSize:14.0f];
[_backButton setBackgroundImage:[UIImage
imageNamed:@"backButton.png"]
forState:UIControlStateNormal];
[_backButton setBackgroundImage:[UIImage imageNamed:@"DownButton.png"] forState:UIControlStateNormal];
[_toolBar addSubview:_backButton];