将位置更改为更靠近 UINavigationBar 中 rightBarButtonItem 的右侧 [英] Change position closer to the right of rightBarButtonItem in UINavigationBar
本文介绍了将位置更改为更靠近 UINavigationBar 中 rightBarButtonItem 的右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
- (void)createCustomBtnRightBar:(UIImage *)buttonImage{self.navigationItem.rightBarButtonItem = nil;UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];如果(按钮图像!= nil){[button setImage:buttonImage forState:UIControlStateNormal];button.frame = CGRectMake(0, 0, 45, 33);button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);} 别的 {[button setTitle:@"Add New Pal" forState:UIControlStateNormal];button.frame = CGRectMake(0, 0, 110, 24);[button.titleLabel setTextAlignment:NSTextAlignmentRight];}[button addTarget:self action:@selector(onClickBtnSendClip:) forControlEvents:UIControlEventTouchUpInside];[按钮 setBackgroundColor:[UIColor greenColor]];//创建一个 UIBarButtonItem 与按钮作为自定义视图UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];self.navigationItem.rightBarButtonItem = customBarItem;}
将宽度更改为 200 时
当添加button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
在所有这些情况下,右边的区域都不能移动
我需要这样的东西.
我什至试过这个
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];customBarItem.customView = 按钮;
但是会崩溃
*** 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:固定和灵活空间项不允许作为单独的导航栏按钮项.请使用 rightBarButtonItems(复数)属性.*** 首先抛出调用堆栈:(0 核心基金会 0x00000001132a2e65 __exceptionPreprocess + 1651 libobjc.A.dylib 0x0000000112d19deb objc_exception_throw + 482 CoreFoundation 0x00000001132a2d9d +[NSException raise:format:] + 2053 UIKit 0x000000011156d560 -[UINavigationItem setRightBarButtonItem:animated:] + 1314 sdsd sdsdsd 0x000000010e042dd3 -[SendClipViewController createCustomBtnRightBar:] + 1475
<块引用>
当我是故事板直接的按钮栏并且右侧的空间更小(根据我的需要)时,我之前工作得很好.
任何帮助将不胜感激.谢谢
解决方案
你需要使用 .Fixed Space 来实现:
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];negativeSpacer.width = -10;self.navigationItem.rightBarButtonItems = @[negativeSpacer, customBarItem];
This is my code
- (void)createCustomBtnRightBar:(UIImage *)buttonImage
{
self.navigationItem.rightBarButtonItem = nil;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
if (buttonImage != nil) {
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 45, 33);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
} else {
[button setTitle:@"Add New Pal" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 110, 24);
[button.titleLabel setTextAlignment:NSTextAlignmentRight];
}
[button addTarget:self action:@selector(onClickBtnSendClip:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor greenColor]];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = customBarItem;
}
When Change width to 200
When add button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
In all these cases, the area of the right can not move
I need something like this.
I even tried this
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
customBarItem.customView = button;
but does crash
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Fixed and flexible space items not allowed as individual navigation bar button item. Please use the rightBarButtonItems (that's plural) property.'
*** First throw call stack:
(
0 CoreFoundation 0x00000001132a2e65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000112d19deb objc_exception_throw + 48
2 CoreFoundation 0x00000001132a2d9d +[NSException raise:format:] + 205
3 UIKit 0x000000011156d560 -[UINavigationItem setRightBarButtonItem:animated:] + 131
4 sdsd sdsdsd 0x000000010e042dd3 -[SendClipViewController createCustomBtnRightBar:] + 1475
Worked perfect before when I was the button bar direct from the story board and space on the right was smaller (as I need).
Any help would be appreciated. Thanks
解决方案
You need to use .Fixed Space to achieve this:
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = @[negativeSpacer, customBarItem];
这篇关于将位置更改为更靠近 UINavigationBar 中 rightBarButtonItem 的右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文