ios - 如何更改 BarButtonItem 的水平位置

标签 ios objective-c uinavigationbar uinavigationitem

我可以使用 setBackgroundVerticalPositionAdjustment 更改自定义栏按钮项的 y,但是如何更改水平位置?

[viewController.navigationItem.rightBarButtonItem setBackgroundVerticalPositionAdjustment:50 forBarMetrics:UIBarMetricsDefault];

最佳答案

您可以使用自定义 View 创建条形按钮,并在该 View 中创建自己的布局。它可以只是简单的 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];

或者你可以在你的酒吧项目之后或之前在酒吧中添加一些固定空间
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] 
                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                        target:nil 
                                        action:nil];
[fixedSpace setWidth:20];

关于ios - 如何更改 BarButtonItem 的水平位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19242510/

相关文章:

uinavigationbar - 使用 UINavigationBar 和 UIBarButtonItem 自动布局

objective-c - 如何动态覆盖UINavigationBar中的drawRect方法?

ios - 导航栏中的分段控件

ios - 如何在应用程序中实现 'User' 功能?

ios - 快速设置模型类属性的值

iphone - 选择 UITableViewCell 时 UILabel 背景被遮挡

ios - 如何在 iOS 上将 flac 转换为 wav?

ios - Swift - 加载搜索结果时的并发问题。我加载图像,发送请求,但请求为时已晚

ios - 如何使用 NSMutableArray 填充 TableView

iphone - 没有边框的圆角矩形 UIButton