如何更改UItabbar图标图像?

发布于 2024-11-09 14:33:52 字数 109 浏览 0 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

|煩躁 2024-11-16 14:34:12

首先,您创建以下代码的类:

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)

}

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 didload you call function :

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)

}
云雾 2024-11-16 14:34:08

你可以像这样改变它。
在选项卡控制器委托方法中,

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

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

通过此您可以更改选项卡项目图像。

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

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

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.

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

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
a√萤火虫的光℡ 2024-11-16 14:34:06

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

以编程方式执行此操作:-

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

使用此方法或如果您正在视觉上执行此操作:-
单击 IB 中的特定选项卡图标,然后选择自定义和图标并给出特定图标文件的名称。

愿这能解决您的问题...

There are always two ways of doing this by programmatically or by using visually(using 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.

May this will solve your problem...

羁〃客ぐ 2024-11-16 14:34:04

如果您想更改 UITabbarItem< 的图像/a> 你可以使用它的实例方法

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

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

- (id)initWithTitle:(NSString  *)title image:(UIImage  *)image tag:(NSInteger)tag
一笔一画续写前缘 2024-11-16 14:34:01

在使用 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;
}  

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

- (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;
}  
緦唸λ蓇 2024-11-16 14:33:59

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;  }

报纸.png 是我在选项卡栏中自己的图像...

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

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

- (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...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文