【问题标题】:How to change the UItabbar icon image?如何更改 UItabbar 图标图像?
【发布时间】:2011-09-01 15:23:01
【问题描述】:

我正在开发一个应用程序并希望自定义 UItabbar 图标图像。

我有一个名为 about.png 的图像,我想将此图像设置为我的应用程序 UItabbar 的左图标图像。 我该怎么做?

【问题讨论】:

    标签: iphone objective-c xcode xcode4 uitabbarcontroller


    【解决方案1】:

    k 您使用此代码并使用您自己的图像,而不是内置的使用您的图像...

    - (id)init {
    UIImage* anImage = [UIImage imageNamed:@"newspaper.png"];
    UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"News" image:anImage tag:0];
    self.tabBarItem = theItem;
    [theItem release];
    return self;  }
    

    newspaper.png 是我自己在标签栏中的图片...

    现在很好,这足以解决您的问题...

    【讨论】:

    • 我可以在委托实现文件中使用此代码吗?它会生成有关 tabBarItem 的错误
    【解决方案2】:

    在您使用选项卡的 viewController 中实现 init()。

    - (id)init {
    
    if (self = [super initWithNibName:@"Search" bundle:nil]) {
        UIImage* tabImage = [UIImage imageNamed:@"search.png"];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Search" image:tabImage tag:0];
        self.tabBarItem = theItem;
        [theItem release];
    }
    return self;
    }  
    

    【讨论】:

      【解决方案3】:

      如果你想改变UITabbarItem的图像,你可以使用它的实例方法

      - (id)initWithTitle:(NSString  *)title image:(UIImage  *)image tag:(NSInteger)tag
      

      【讨论】:

        【解决方案4】:

        总是有两种方法可以通过编程方式或通过视觉方式(使用 IB)实现这一点

        以编程方式:-

        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"News" image:anImage tag:0];
        

        使用这个或者如果你在视觉上做:- 单击 IB 中的特定选项卡图标并选择自定义和图标并给出特定图标文件的名称。

        希望这能解决你的问题...

        【讨论】:

        • 我并不是说你使用了 Xcode 的内置图像,只是看看我在你的列表中发布的新答案......
        【解决方案5】:

        你可以改变它。 在标签栏控制器委托方法中

        - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
        
        {
            if([tabBarController selectedIndex] == 0)
            {
                [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
            }    
        }
        

        通过这个您可以更改您的 tabbaritem 图像。

        或者您可以直接在视图控制器中使用 init(或 ViewWillAppear)方法,例如

                [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
        

        【讨论】:

          【解决方案6】:

          首先你要编写这个代码:

          extension UITabBarController {
          
              func addIcon(icon : UIImage, deselectIcon : UIImage, buttonIndex : Int, currentPage : Int){
          
                  var index = 0
                  for view in view.subviews {
          
                      if view.subviews.count > 3 {
          
                          for _view in view.subviews {
          
                              if index == buttonIndex {
          
                                  for v in _view.subviews {
          
                                      if v is UIImageView {
                                          v.removeFromSuperview()
                                      }
                                  }
          
                                  let img = UIImageView(frame: CGRect(x: _view.frame.width / 2 - 15, y: 4, width: 30, height: 30))
          
                                  if buttonIndex == currentPage {
                                      img.image = icon
                                  }else{
                                      img.image = deselectIcon
                                  }
                                  img.contentMode = .scaleAspectFit
          
                                  _view.addSubview(img)
          
          
                              }
                              index += 1
                          }
          
                      }
                  }
          
              }
          }
          

          didload你调用函数:

          override func viewDidLoad() {
              super.viewDidLoad()
          
          
          
              DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1 ) {
                  self.tabBarController?.addIcon(icon: #imageLiteral(resourceName: "Shop"), deselectIcon: #imageLiteral(resourceName: "Shop"), buttonIndex: 1, currentPage: 1)
              }
          }
          
          override func viewWillAppear(_ animated: Bool) {
          
              tabBarController?.addIcon(icon: #imageLiteral(resourceName: "Shop"), deselectIcon: #imageLiteral(resourceName: "Shop"), buttonIndex: 1, currentPage: 1)
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-02-11
            • 2014-06-17
            • 1970-01-01
            • 2021-10-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-10
            相关资源
            最近更新 更多