我需要在iPhone导航栏中显示待处理通知的数量。外观应类似于通知徽章 - 但这些不是 APNS 通知。它们是从具有类似目的的私人服务器发送的。
我尝试在我的UINavigationbar
中添加一个右/左按钮(UIBarButtonItem
),但它的外观似乎非常僵硬。我无法设置其宽度、字体等。查看我的代码:
self.notifButton = [[UIBarButtonItem alloc] initWithTitle:@"0" style:
UIBarButtonItemStyleBordered target:self action:@selector(TouchNotif)];
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:self.notifButton];
self.navigationItem.rightBarButtonItems = items;
由于其他 2 个项目也添加到项目数组中,导航栏杂乱无章。它们的字体,宽度等我无法使用,或者我不知道应该如何创建它们。
我的问题:
1)在导航栏右侧区域中容纳至少3个项目的正确方法是什么?我问这个是因为我找不到一种方法来玩我使用的UIButtons
的宽度和字体。
2)如果我想为我的通知按钮自定义外观(就像通知徽章一样) - 是否有任何指示如何制作?使用哪个控件,如何设置UINavigationBar
中允许的框架和字体?
请帮忙。
使用 initWithCustomView
创建一个包含自定义视图的UIBarButtonItem
。自定义视图可以是带有数字徽章作为子视图的自定义 UIButton。使用此自定义视图,您还可以控制按钮的宽度。
没有用于直接创建通知徽章的公共 API。如果是标签栏项,您可以使用属性badgeValue
设置徽章 - 但不能使用 UIBarButtonItem
.在这里,您需要使用此开源控件:MKNumberBadgeView。
请注意,属性rightBarButtonItems
从 iOS 5 开始可用。如果您只需要一个项目,请改为设置rightBarButtonItem
。
UIButton * buttton = [UIButton buttonWithType:UIButtonTypeCustom];
[buttton setFrame:CGRectMake(285, 20, 20, 20)];
[buttton.layer setCornerRadius:10];
[buttton setTitle:@"23" forState:UIControlStateNormal];
[buttton.titleLabel setFont:[UIFont systemFontOfSize:12]];
[buttton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[buttton setBackgroundColor:[UIColor whiteColor]];
[self.navigationController.view addSubview:buttton];
首先在导航中获取相应的栏按钮项
控制器let baritem = navigationItem.right/leftBarButtonItem
baritem.badgeValue = "(correspondingValues)"