我在我的应用程序中使用 UINavigationController 及其栏。
现在我想更改 leftBarButtonItem 和 rightBarButtonItem 的位置。
我希望它们位于不同的 x 和 y 位置,具有自定义的宽度和高度。
但问题是,更改框架不会改变 barButtonItem 的任何内容。我现在不知道为什么,但框架总是会重置为其原始坐标。
这是我在 viewWillAppear 中调用的代码:
UINavigationItem *navItem = [[self.navigationBar items] lastObject];
UIView *rightview = [[UIView alloc] initWithFrame:CGRectMake(0,0,66,30)];
rightview.backgroundColor = [UIColor blueColor];
//add custom view
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:rightview];
navItem.leftBarButtonItem = search;
视图不是从 0/0 开始,例如 x 位置是 0 之前的 5px。
关于如何解决这个问题有什么建议吗?
这对我的 leftBarButtonItem 有用:
如果您希望 UIBarButtonItem 精确显示在位置 (0, 0),您可以创建一个 UIButton,将其框架设置为 (-5, 0, width, height),假设左侧栏按钮项的偏移量为 (5, 0),并将其放置在您设置为 leftBarButtonItem 的 UIView 中。
#import "UINavigationItem+CSAButtons.h"
@implementation UINavigationItem (CSAButtons)
- (void) makeLeftBarButtonWithTarget:(id)target action:(SEL)action {
UIImage * imageNormal = [UIImage imageNamed:@"normal.png"];
UIImage * imageHighlighted = [UIImage imageNamed:@"highlighted.png"];
UIImage * imageDisabled = [UIImage imageNamed:@"disabled.png"];
// create your button
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.exclusiveTouch = YES;
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:imageNormal forState:UIControlStateNormal];
[button setBackgroundImage:imageHighlighted forState:UIControlStateHighlighted];
[button setBackgroundImage:imageDisabled forState:UIControlStateDisabled];
// set the frame of the button (better to grab actual offset of leftbarbuttonitem instead of magic numbers)
button.frame = CGRectMake(-5.0, 0.0, imageNormal.size.width, imageNormal.size.height);
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageNormal.size.width, imageNormal.size.height)];
[view addSubview:button];
// set the barbuttonitem to be the view
UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.leftBarButtonItem = barButtonItem;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)