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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:32:08  来源:igfitidea点击:

Can I change the position of navigationbar item?

iphoneios

提问by u1053236

Here is the snapshot: enter image description here

这是快照: 在此处输入图片说明

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 UIBarButtonItemwith system item UIBarButtonSystemItemFixedSpacewith -5 width which will remove 5 pxpadding. Then set leftBarButtonItemswith 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 UIBarButtonSystemItemFlexibleSpaceitem 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];

回答by Saul Trejo

I had a similar issue and found an option that sufficed in the storyboard. Just click on the navbar item and you should see the x/y position options.

我有一个类似的问题,并在故事板中找到了一个足够的选项。只需单击导航栏项目,您就会看到 x/y 位置选项。

enter image description here

在此处输入图片说明