【问题标题】:ios back button in the barios 栏中的后退按钮
【发布时间】:2011-10-03 13:29:49
【问题描述】:

是否可以在工具栏中插入一个工具按钮,其形状类似于导航栏中使用的后退按钮? 这就是我的意思:

提前致谢!

【问题讨论】:

  • 我不确定我是否理解您想要达到的目标。您是否尝试在功能上将工具栏和导航栏组合成一个栏?
  • 不抱歉,在我看来我只有一个工具栏,但我需要创建一个像背面这样的按钮,但是只有当我使用导航栏时,带有箭头形状的背面才处于活动状态,但我只有一个工具栏
  • 好的,所以您希望工具栏中的后退按钮只有在您将某些控制器推入导航堆栈时才会激活。是这样吗?
  • 是的,我没有导航栏,只有工具栏:)
  • 我会在几分钟后添加我的答案。坐稳。

标签: ios button toolbar back-button shape


【解决方案1】:

你不能这样做,至少不能使用后退按钮的形状,即左侧的箭头末端。 backBarButtonItem 只是 UINavigationItem 的一个属性。

您可以使用工具栏上的矩形按钮返回(尽管我认为 Apple 不喜欢这样做...您应该阅读 iOS 人机界面指南),

您可以在屏幕的其他位置添加自定义后退按钮。例如,如果您的工具栏位于底部,您可以在屏幕的左上角添加自定义后退按钮以保持整洁。这样,您将节省仅用于返回按钮的导航栏的屏幕空间。

【讨论】:

  • 使用 UIButton 类,也许?...将它的框架设置到您需要的任何位置,设置它的 backgroundImage(具有后退箭头的形状,或其他东西)等等。
【解决方案2】:

typeonerror 在 github 上有包含您需要的代码;它实际上是用于自定义视图控制器上的后退按钮,但您可以使用它在工具栏上创建后退按钮:

https://github.com/typeoneerror/BBCustomBackButtonViewController

尤其是如下代码(实际上来自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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多