【问题标题】:use [UINavigationBar appearance] to change back button image使用 [UINavigationBar 外观] 更改返回按钮图像
【发布时间】:2012-05-03 18:43:20
【问题描述】:

我想更改 UINavigationBar 的后退按钮

我可以使用以下代码:

 // Set the custom back button
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
    button.adjustsImageWhenDisabled = NO;


    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 30, 30);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
    self.navigationItem.hidesBackButton = YES;

    // Cleanup
    [customBarItem release];

但我必须将该代码放入每个 viewDidLoad 方法中。我想为整个程序做一次。

我的尝试是这样的:

 UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5
                             resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)];
    UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"] 
                             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 
                                                      forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    //[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
    //                forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
                                                      forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
    [attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
    // [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont];
    [attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
    [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted];

但它会拉伸图像并在其上绘制文本,这是我不想要的。我只想要每个视图中大小相同的图像。

谢谢!

【问题讨论】:

    标签: iphone uinavigationbar back-button uinavigationitem


    【解决方案1】:

    从 iOS 5.0 开始,您可以使用全局外观修饰符来更改整个应用中的所有后退按钮:

    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
                                            forState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];
    

    为了获得良好的效果,背景图片必须是可调整大小的图片。

    【讨论】:

      【解决方案2】:

      这可能会奏效。在顶部的你的 appdelegate 中添加以下内容。

          @implementation UIViewController (CustomFeatures)
      -(void)setNavigationBar{
          // Set the custom back button
          UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];
      
          //create the button and assign the image
          UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
          [button setImage:buttonImage forState:UIControlStateNormal];
          [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
          button.adjustsImageWhenDisabled = NO;
      
      
          //set the frame of the button to the size of the image (see note below)
          button.frame = CGRectMake(0, 0, 30, 30);
      
          [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
      
          //create a UIBarButtonItem with the button as a custom view
          UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
          self.navigationItem.leftBarButtonItem = customBarItem;
          self.navigationItem.hidesBackButton = YES;
      
          // Cleanup
          [customBarItem release];
      }
      @end
      

      并在您的viewDidLoad 中调用[self setNavigationBar];

      【讨论】:

      • 可以,但是当我在视图之间来回移动时按钮会闪烁。没什么大不了的,但如果它可以过渡更清洁,那就太好了
      • 还有后面的动作方法在哪里?
      • Nice hack!,返回方法 swift self.navigationController?.popViewController(animated: true)
      【解决方案3】:

      使用此代码并享受...

      UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [leftButton setUserInteractionEnabled:NO];
      [leftButton setImage:[UIImage imageNamed:@"backag.png"] forState:UIControlStateNormal];    
      leftButton.frame = CGRectMake(0, 0, 30, 30);
      [leftButton addTarget:self action:@selector(YourclickeventClick:) forControlEvents:UIControlEventTouchUpInside];        
      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
      [leftbutton release];
      

      希望对你有所帮助..

      【讨论】:

        【解决方案4】:

        您可以为 backBarButtonItem 创建一个自定义按钮,然后对其进行自定义。

        转到这个问题 - How do you change color/image on the default backBarButtonItem?,您可以在那里找到答案。

        要使条形按钮项看起来像 backBarButtonItem,请参阅此答案。 Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar

        【讨论】:

          【解决方案5】:

          这是 Swift

          的有效解决方案
              let backImage = UIImage(named: "back")
              UINavigationBar.appearance().backIndicatorImage = backImage
              UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
              UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
                  UIColor.white], for: .normal)
              UIBarButtonItem.appearance().tintColor = UIColor.green //if you want to change color
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-06-15
            • 2012-11-23
            • 2020-11-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多