ios 我可以更改导航栏项目的位置吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9866062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I change the position of navigationbar item?
提问by u1053236
Here is the snapshot:
这是快照:
And the code is here:
代码在这里:
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0.0f, 0.0f, 46.0f, 32.0f);
[leftButton setBackgroundImage:[UIImage imageNamed:@"navi_register_up.png"] forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(doRegister) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftBarItem;
[leftBarItem release];
Is there any way to move the button up some px? Thank you:)
有没有办法将按钮向上移动一些像素?谢谢:)
回答by saadnib
You need to create a UIBarButtonItem
with system item UIBarButtonSystemItemFixedSpace
with -5 width which will remove 5 px
padding. Then set leftBarButtonItems
with both fixed space item and your back bar item like this -
您需要创建一个宽度为 -5 的UIBarButtonItem
系统项UIBarButtonSystemItemFixedSpace
,这将删除5 px
填充。然后像这样设置leftBarButtonItems
固定空间项目和你的后栏项目 -
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-5];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBarItem,nil];
回答by xda1001
You can try this,
你可以试试这个
leftButton.imageEdgeInsets = UIEdgeInsetsMake(-2, 0, 0, 0);
回答by WrightsCS
Add a UIBarButtonSystemItemFlexibleSpace
item in between the items you want to separate.
UIBarButtonSystemItemFlexibleSpace
在要分开的项目之间添加一个项目。
回答by JP Aquino
Here's a Swift 3 version of the most voted answer:
这是投票最多的答案的 Swift 3 版本:
let soundButton = UIBarButtonItem(image:image , style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.soundPressed))
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -5
self.navigationItem.rightBarButtonItems = [negativeSpacer,soundButton]
回答by John Estropia
The height of a standard bar button is 29pt. Do yourself a favor and just shrink leftbutton
's height and navi_register_up.png
's height instead.
标准条形按钮的高度为 29pt。帮自己一个忙,只是缩小leftbutton
的高度 和navi_register_up.png
的高度。
回答by iphone_Kanta
You can use this code,I think this will be helpful to you. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0,0,50,32); [button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
您可以使用此代码,我认为这会对您有所帮助。UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0,0,50,32); [button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
[backButtonView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
//barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
[self.navigationItem setLeftBarButtonItem:barButtonItem];