UIBarButtonItem
UIBarButtonItem 表示导航条上的一个按钮。导航条是 UINavigationController 在屏幕顶部画的一条,上面有正中的标题、左侧的返回按钮、左右两侧的按钮。用户点了按钮,按钮派发 click 事件。
导航条上的东西由三个类描述:
UINavigationItem表示一个页面在导航条上的配置:标题、副标题、返回按钮上的字、左右两侧各有哪些按钮。每个页面自带一个,从页面的navigationItem读到。UIBarButtonItem(本页)表示其中的一个按钮。UIBarItem是UIBarButtonItem和UITabBarItem共同的父类,提供title、image、isEnabled、tag四个属性。
按钮放进页面的 rightBarButtonItems 或 leftBarButtonItems 才显示。
继承关系:EventTarget → NSObject → UIBarItem → UIBarButtonItem
怎么在导航条上放按钮、改标题和返回按钮,见指南在导航条上放按钮和标题。
构造函数
创建一个按钮,可以同时给出文字、图标、样式,或者一个代替按钮的视图。
静态方法
| 方法 | 说明 |
|---|---|
UIBarButtonItem.systemItem() | 创建一个系统预置的按钮,比如加号、分享、完成 |
UIBarButtonItem.flexibleSpace() | 创建一个宽度自动伸缩的空白 |
UIBarButtonItem.fixedSpace() | 创建一个固定宽度的空白 |
实例属性
还继承 UIBarItem 的属性:title、image、isEnabled、tag。
| 属性 | 说明 |
|---|---|
style | 样式:普通,或者强调这个页面最主要的操作 |
width | 宽度。只对固定宽度的空白有意义 |
isHidden | 暂时不显示 |
customView | 代替系统画的按钮的视图 |
事件
| 事件 | 什么时候派发 |
|---|---|
click | 用户点了这个按钮 |
按钮不是视图
UIBarButtonItem 只描述「这里要一个按钮,上面写什么、是什么样式」,由系统按这份描述画出按钮。它不是 UIView:
- 没有
frame、backgroundColor,不能addSubview()。按钮的位置和大小由系统安排。 - 没有
UIControl的那些触摸事件,只有一个click。
要在导航条上放一个完全自己画的东西,把一个视图交给 customView。
示例
编辑页右上角的「保存」
从空白 App 开始:首页上有一个按钮,点它进入「编辑」页;「编辑」页的导航条右侧有一个「保存」按钮,点它在控制台输出一行并返回首页。
js
application.addEventListener('sceneconnect', (e) => {
const win = new UIWindow(e.scene);
const home = new UIViewController();
home.title = '首页';
home.view.backgroundColor = 'systemBackground';
const nav = new UINavigationController(home);
win.rootViewController = nav;
win.makeKeyAndVisible();
function makeEditor() {
const page = new UIViewController();
page.title = '编辑';
page.view.backgroundColor = 'systemBackground';
const save = new UIBarButtonItem({ title: '保存', style: 'prominent' });
save.addEventListener('click', () => {
console.log('保存了');
nav.popViewController({ animated: true });
});
page.navigationItem.rightBarButtonItems = [save];
return page;
}
const open = UIButton.buttonWithType('system');
open.setTitle('去编辑', { forState: ['normal'] });
open.translatesAutoresizingMaskIntoConstraints = false;
home.view.addSubview(open);
NSLayoutConstraint.activateConstraints([
open.centerXAnchor.constraintEqualToAnchor(home.view.centerXAnchor),
open.centerYAnchor.constraintEqualToAnchor(home.view.centerYAnchor),
]);
open.addEventListener('click', () => {
nav.pushViewController(makeEditor(), { animated: true });
});
});效果
- 首页的导航条正中是「首页」,左右两侧没有按钮。
- 点「去编辑」,进入编辑页。导航条正中是「编辑」,左侧是写着「首页」的返回按钮,右侧是「保存」按钮。
- 点「保存」,控制台输出
保存了,屏幕回到首页。
导航条右侧的按钮能显示、能点击,在验收用例里用真实的点击验证过。'prominent' 样式画出来的样子未在模拟器验证。这段代码没有作为一个整体在模拟器运行过,其中用到的每个 API 都在验收用例里运行过。
对应的 iOS API
| ObjC | JS |
|---|---|
[[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleProminent target:self action:@selector(save)] | new UIBarButtonItem({ title: '保存', style: 'prominent' }),再 addEventListener('click', save) |
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:… action:…] | UIBarButtonItem.systemItem('add') |
[[UIBarButtonItem alloc] initWithCustomView:v] | new UIBarButtonItem({ customView: v }) |
[UIBarButtonItem flexibleSpaceItem] | UIBarButtonItem.flexibleSpace() |
[UIBarButtonItem fixedSpaceItemOfWidth:20] | UIBarButtonItem.fixedSpace(20) |
item.style = UIBarButtonItemStyleDone | item.style = 'done'(读回 'prominent') |
item.enabled = NO | item.isEnabled = false |
item.hidden = YES | item.isHidden = true |
item.target / item.action | 不开放。用 addEventListener() |
UIBarButtonItemStyleBordered | 不开放。Apple 已废弃 |
参见
- 在导航条上放按钮和标题:放按钮、图标、预置按钮、禁用、返回按钮
UINavigationItem:按钮放在哪里UIBarItem:父类,title、image、isEnabled、tagUINavigationController:导航条由它显示UIButton:页面里的按钮。点击事件同名