【问题标题】:how can I change the horizontal position of BarButtonItem如何更改 BarButtonItem 的水平位置
【发布时间】:2013-10-08 08:15:38
【问题描述】:
我可以使用 setBackgroundVerticalPositionAdjustment 更改自定义栏按钮项的 y,但是如何更改水平位置?
[viewController.navigationItem.rightBarButtonItem setBackgroundVerticalPositionAdjustment:50 forBarMetrics:UIBarMetricsDefault];
【问题讨论】:
标签:
ios
objective-c
uinavigationbar
uinavigationitem
【解决方案1】:
您可以使用自定义视图创建条形按钮,并在该视图中创建自己的布局。可以只是简单的UIButton,也可以在UIButton中设置edgeInsets。
见Customization of UINavigationBar and the back button
f.e.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"chk_back.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(BackBtn) forControlEvents:UIControlEventTouchUpInside];
UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 50, 38)] autorelease];
btn.frame = CGRectMake(10, 3, 30, 32); // where you can set your insets
[customView addSubview:btn];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:customView];
或者您可以在 Bar 中的 bar item 之后或之前添加一些 fixedSpace
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[fixedSpace setWidth:20];
【解决方案2】:
您可以更改imageInsets 的rightBarButtonItem。
例如:
[viewController.navigationItem.rightBarButtonItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 50)];