Cusomized UINavigationBar要求我提供一个自定义的“返回”按钮,我使用navigationItem.leftBarButtonItem = myCustomizedButton
,但它的位置是固定的。在iOS上,我如何将navigationItem.leftBarButtonItem水平向右移动?
是否有人会这么友善地分享我该如何将这个按钮向右移40像素?
Cusomized UINavigationBar要求我提供一个自定义的“返回”按钮,我使用navigationItem.leftBarButtonItem = myCustomizedButton
,但它的位置是固定的。在iOS上,我如何将navigationItem.leftBarButtonItem水平向右移动?
是否有人会这么友善地分享我该如何将这个按钮向右移40像素?
您可以创建一个包含比您的图像大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;
您可以为显示在导航栏上的图片添加空白区域。我遇到了同样的问题,而且这是我找到解决它的唯一解决方案。有点棘手,但它的工作原理...