开发者

on iOS, how can I shift navigationItem.leftBarButtonItem horizontally right?

A cusomized UINavigationBar requires me to present a customized "back" button, I use navigationItem.leftBarButtonItem = myCustomize开发者_JS百科dButton, but its position is fixed.

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


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;


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜