简体   繁体   中英

How to change the UItabbar icon image?

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?

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

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"]];

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)

}

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM