我可以更改导航栏项目的位置吗?

时间:2012-03-26 02:55:44

标签: iphone ios

以下是快照: enter image description 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];

有没有办法将按钮向上移动一些px? 谢谢:))

7 个答案:

答案 0 :(得分:70)

您需要使用系统项UIBarButtonItem创建一个UIBarButtonSystemItemFixedSpace,其宽度为-5,这将删除5 px填充。然后使用固定空间项目和后栏项目设置leftBarButtonItems,如下所示 -

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-5];

self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBarItem,nil];

答案 1 :(得分:13)

你可以试试这个,

leftButton.imageEdgeInsets = UIEdgeInsetsMake(-2, 0, 0, 0);

答案 2 :(得分:4)

在要分隔的项目之间添加UIBarButtonSystemItemFlexibleSpace项。

答案 3 :(得分:1)

以下是投票最多的答案的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]

答案 4 :(得分:0)

标准栏按钮的高度为29pt。帮自己一个忙,只需缩小leftbutton的身高和navi_register_up.png的身高。

答案 5 :(得分:0)

您可以使用此代码,我认为这会对您有所帮助。 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];

答案 6 :(得分:0)

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

enter image description here