刚刚学ios不久,好多东西还是要靠百度,今天在设置右上角刷新按钮时 ,自定义的 bar 感觉写了好多代码,所以找了一下原生自带的方法,现做以下记录
我们先看看是怎么用的,第一步,了解初始化方法
初始化方法提供了设置图片,标题,系统自带样式,自定义视图等,看下 其他的初始化方法
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (instancetype)initWithCustomView:(UIView *)customView;
那系统提供了哪些呢,有了系统系统的图标,样式等,我们能省不少事呢
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
看变量名我们也知道是什么作用的样式了,系统提供不一定能满足所有人的需要,app风格,自带的太丑等因素,都需要我们洗定义,我们看两个例子
//自定义文字样式的左侧按钮
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithTitle:@"取消"style:UIBarButtonItemStyleBordered target:selfaction:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = btnItem;
//自定义图片样式的右侧按钮
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_connect_refresh.png"] style:UIBarButtonItemStylePlain target:self action:@selector(refreshTap:)];
self.navigationItem.rightBarButtonItem=btnItem;
是不是很简单呢