iOS UIBarButtonItem間距、title點擊大小改變

UIBarButtonItem的使用

一、initWithTitle ?字體大小、顏色

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"說明" style:UIBarButtonItemStylePlain target:self action:@selector(onRightButtonClick)];

? ? item.tintColor = [UIColor whiteColor];

? ? self.navigationItem.rightBarButtonItem = item;

? ? [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];

? ? [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateHighlighted];

二、initWithCustomView?兩個Item之間的間距

? ? UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

? ? [btn1 addTarget:self action:@selector(onSZRightButtonClick) forControlEvents:UIControlEventTouchUpInside];

? ? [btn1 setImage:[UIImage imageNamed:@"icon-wd-sz"] forState:UIControlStateNormal];

? ? [btn1 sizeToFit];

? ? UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];

? ? UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

? ? fixedSpaceBarButtonItem.width = 15;

? ? UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

? ? [btn2 addTarget:self action:@selector(onXXRightButtonClick) forControlEvents:UIControlEventTouchUpInside];

? ? [btn2 setImage:[UIImage imageNamed:@"icon-wd-xx"] forState:UIControlStateNormal];

? ? [btn2 sizeToFit];

? ? UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:btn2];

? ? self.navigationItem.rightBarButtonItems? = @[item2,fixedSpaceBarButtonItem,item1];

三、initWithImage 如果放兩個的話,間距不能控制,改用initWithCustomView

? ? UIImage *image = [[UIImage imageNamed:@"icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

? ? UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(onLeftButtonClick)];

? ? self.navigationItem.leftBarButtonItem = leftBarButton;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容