xcode 如何更改 UItabbar 图标图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6122779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:06:42  来源:igfitidea点击:

How to change the UItabbar icon image?

iphoneobjective-cxcodexcode4uitabbarcontroller

提问by Faheem Rajput

I am working on an app and want to customize the UItabbar icon image.

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

I have an image with name about.png this image i want to set as left icon image of my app's UItabbar. how can i do this?

我有一个名为 about.png 的图像,我想将此图像设置为我的应用程序 UItabbar 的左侧图标图像。我怎样才能做到这一点?

采纳答案by user755278

k you use this code and used your own images not the built in one's used your images...

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 is my own image in the tab bar...

news.png 是我自己在标签栏中的图像...

k fine now this will be sufficient for your problem...

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

回答by Muhammad Rizwan

You can change it like. in tabbar controller delegate method

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

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

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

through this you can change your tabbaritem image.

通过这个你可以改变你的 tabbaritem 图像。

Or you can use directly in your view controllers init(or ViewWillAppear) method, like

或者你可以直接在你的视图控制器中使用 init(or ViewWillAppear) 方法,比如

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

回答by kambiz dehgane

First you make class this code :

首先你让这个代码类:

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
                }

            }
        }

    }
}

In didloadyou call function :

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)

}

回答by visakh7

If you want to change the image for UITabbarItemyou can make use of its instance method

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

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

回答by Nitish

Implement init() in viewController in which you are using tab.

在您使用选项卡的 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;
}  

回答by user755278

There are always two ways of doing this by programmatically or by using visually(using IB)

总是有两种方法可以通过编程或视觉使用(使用 IB)

Programmatically:-

以编程方式:-

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

use this or if you are doing visually:- Click on the particular tab icon in the IB and chose customize and icon and give the name of particular icon file.

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

May this will solve your problem...

可能这会解决你的问题...