我的应用程序中有一个导航栏,有两个按钮,每侧一个。此按钮用于更改应用程序的某些重要方面,如服务器和内容。
当服务器脱机或出现任何问题时,系统会要求用户更改此选项。
我想要的是:如何将图像附加到按钮以引起用户的注意?像这样:
但问题是:我甚至不知道如何开始寻找这个,我该怎么做?
答案 0 :(得分:1)
我建议添加UILabel作为按钮的子视图而不是图像,这样您就可以通过在应用程序的任何位置使用NSNotificationCenter以编程方式更改它。
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateButtonLabel:)
name:@"UpdateButtonLabel" object:nil];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]
button.frame = CGRectMake(0, 0, 35, 35);
[button addTarget:self action:@selector(buttonMethods:) forControlEvents:UIControlEventTouchUpInside];
[button setTintColor:[UIColor companyBlue]];
[button setTitle:@"Button" forState:UIControlStateNormal];
self.buttonLabel = [[UILabel alloc]initWithFrame:CGRectMake(18,-7,18,18)];
self.buttonLabel.textColor = [UIColor whiteColor];
self.buttonLabel.backgroundColor = [UIColor companyRed];
self.buttonLabel.font = [UIFont systemFontOfSize:13.0];
self.buttonLabel.layer.cornerRadius = 9;
self.buttonLabel.layer.masksToBounds = YES;
self.buttonLabel.textAlignment = NSTextAlignmentCenter;
self.buttonLabel.text = @"!";
[button addSubview:self.buttonLabel];
}
- (void)updateButtonLabel:(NSNotification *)notification
{
NSString *labelString = notification.object;
// Post a notification from any view controller with a string with no characters to hide the label:
if (newString.length < 1)
{
self.buttonLabel.hidden = YES;
}
else
{
self.buttonLabel.text = labelString;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
答案 1 :(得分:0)
对于此initWithCustomView
,可以使用此功能,您可以在其中添加UIView
个对象
-(void)setRightBarButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 35, 35);
[button addTarget:self action:@selector(buttonMethods:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Button" forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
}