Ale*_*der 4 objective-c uitoolbaritem ios
我在我的应用程序中使用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;
Run Code Online (Sandbox Code Playgroud)

视图不是从0/0开始,例如x位置是5px而不是0.
有关如何解决此问题的任何建议?
小智 12
这对我来说对于leftBarButtonItem:如果你想让UIBarButtonItem精确地显示在位置(0,0),你可以创建一个UIButton,将它的框架设置为(-5,0,宽度,高度),假设它的offet左侧barbutton项是(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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7508 次 |
| 最近记录: |