ios back button in the bar

纵然是瞬间 提交于 2019-12-11 12:02:35

问题


it's possible insert a toolbutton in a tool bar with the shape like the back button used in the navigation bar? here is what I mean:

thanks in advance!


回答1:


You can't do it, at least not with the shape of a back button, the one with the arrow end on the left. backBarButtonItem is only a property of UINavigationItem.

You can either use a rectangular button on your toolbar to go back (although I don't think Apple is fond of that... you should read the iOS Human Interface Guidelines),

OR you can add a custom back button somewhere else on the screen. For instance, if your toolbar is at the bottom you can add a custom back button to the top-left corner of the screen to keep things tidy. This way, you would be saving the screen space of having a navigation bar just for the back button.




回答2:


typeonerror has code on github that contains what you need; it's actually for customising the back button on the a view controller, but you can use it to create a back button on a toolbar instead:

https://github.com/typeoneerror/BBCustomBackButtonViewController

In particular, code like the following (which actually came from stackoverflow, Customization of UINavigationBar and the back button ):

+ (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
{
   UIImage *image = [UIImage imageNamed:@"button_back"];
   image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];

   NSString *title = NSLocalizedString(@"Back", nil);
   UIFont *font = [UIFont boldSystemFontOfSize:12.0f];

   UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
   button.titleLabel.textColor = [UIColor blackColor];

   CGSize textSize = [title sizeWithFont:font];
   CGFloat margin = (button.frame.size.height - textSize.height) / 2;
   CGFloat marginRight = 7.0f;
   CGFloat marginLeft = button.frame.size.width - textSize.width - marginRight;
   [button setTitleEdgeInsets:UIEdgeInsetsMake(margin, marginLeft, margin, marginRight)]; 
   [button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];   

   return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}


来源:https://stackoverflow.com/questions/6663565/ios-back-button-in-the-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!