在iOS上,如何向右水平移动navigationItem.leftBarButtonItem?

发布于 2024-09-18 06:09:59 字数 145 浏览 2 评论 0原文

自定义的 UINavigationBar 要求我提供自定义的“后退”按钮,我使用 navigationItem.leftBarButtonItem = myCustomizedButton,但其位置是固定的。

有谁能分享一下如何将此按钮向右移动 40 像素吗?

A cusomized UINavigationBar requires me to present a customized "back" button, I use navigationItem.leftBarButtonItem = myCustomizedButton, but its position is fixed.

Would anyone be so kind to share how can I shift this button 40pixels to right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-09-25 06:10:00

您可以创建比图像大 40 像素的包含视图。
以 40 像素偏移添加图像。
添加包含视图作为 leftBarButtonItem。

代码如下:

// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease];

// Create a custom button with the image
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:barButtonImage forState:UIControlStateNormal];
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height);
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

[containingView addSubview:barUIButton];

// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];

// Add the container bar button
navigationItem.leftBarButtonItem = containingBarButton;

You can create a containing view that is 40 pixels bigger than your image.
Add your image at 40 pixels offset.
Add the containing view as the leftBarButtonItem.

Code follows:

// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease];

// Create a custom button with the image
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:barButtonImage forState:UIControlStateNormal];
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height);
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

[containingView addSubview:barUIButton];

// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];

// Add the container bar button
navigationItem.leftBarButtonItem = containingBarButton;
鱼窥荷 2024-09-25 06:10:00

您可以向导航栏上显示的图片添加空白区域。我遇到了同样的问题,这是我找到的唯一解决方案。有点棘手,但它有效......

You can add a blank space to your picture that you display on the navBar. I've had the same problem, and it's the only one solution i've found for resolve it. A little bit tricky but it works...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文