Add image on tabbar programmatically in iOS

谁说我不能喝 提交于 2019-12-11 04:40:43

问题


How to add images on tab bar programmatically in iOS (Xcode 4.2)?

I already asked question but did not get a satisfied answer at Could not set tab bar image in Xcode 4.2 Programmatically.


回答1:


Try the following code

UITabBarItem *tabItem = [[[UITabBarItem tabBar] items] objectAtIndex:yourIndex];
    [tabItem setTitle:@"theTitle"];
    [tabItem setImage:[UIImage imageNamed:@"yourImage.png"]];



回答2:


iOS 5 Appearance api,

UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];

also see this thread, second highest voted answer explains both cases <5.0 and =>5.0 case.




回答3:


Write this method in your ViewController:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.tabBarItem.image = [UIImage imageNamed:@"image.png"];

    }
    return self;
}


来源:https://stackoverflow.com/questions/11392165/add-image-on-tabbar-programmatically-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!