【发布时间】:2010-09-04 16:45:49
【问题描述】:
自定义 UINavigationBar 需要我提供自定义的“返回”按钮,我使用 navigationItem.leftBarButtonItem = myCustomizedButton,但它的位置是固定的。
有人愿意分享我如何将这个按钮向右移动 40 像素吗?
【问题讨论】:
标签: ios uinavigationbar customization
自定义 UINavigationBar 需要我提供自定义的“返回”按钮,我使用 navigationItem.leftBarButtonItem = myCustomizedButton,但它的位置是固定的。
有人愿意分享我如何将这个按钮向右移动 40 像素吗?
【问题讨论】:
标签: ios uinavigationbar customization
您可以创建比图像大 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;
【讨论】:
您可以在导航栏上显示的图片中添加一个空白区域。我遇到了同样的问题,这是我找到的唯一解决方案。有点棘手,但它确实有效......
【讨论】: