在Swift中更改UITabBarItem上的selectedImage

时间:2015-01-11 19:06:28

标签: ios swift uitabbaritem

我一直在尝试更改UITabBar上的所选图像。我已经按照其他Stackoverflow问题列出的程序,但似乎没有任何工作。

我尝试通过“用户定义的运行时属性”部分设置图像,并尝试将以下内容添加到AppDelegate.swift中:

var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]

var selectedImage0 = UIImage(named:"NewsfeedTabSelected")
selectedImage0?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabItems[0].selectedImage = selectedImage0

这不会产生任何结果。我做了一个 println(tabItems [0] .title)并输出了正确的标题,所以我知道对TabBarItem的引用是有用的。

有什么想法吗?

3 个答案:

答案 0 :(得分:10)

我在AppDelegate上使用类似的东西解决了。

var tabBarController = self.window!.rootViewController as UITabBarController
var tabBar = tabBarController.tabBar as UITabBar

var tabBarItem1 = tabBar.items![0] as UITabBarItem
var tabBarItem2 = tabBar.items![1] as UITabBarItem
var tabBarItem3 = tabBar.items![2] as UITabBarItem

tabBarItem1.selectedImage = UIImage(named: "FirstSelectedImage")
tabBarItem2.selectedImage = UIImage(named: "SecondSelectedImage")
tabBarItem3.selectedImage = UIImage(named: "ThirdSelectedImage")

答案 1 :(得分:6)

之前的答案并不完全奏效。 我必须将新的UIImage' UIImageRenderingMode设置为AlwaysOriginal,这样才能解决我的问题。

代码如下:

import UIKit

class MainTab: UITabBarController {

override func viewDidLoad() {

    var tabBar = self.tabBar

    var homeSelectImage: UIImage! = UIImage(named: "firstPageSelected")?.imageWithRenderingMode(.AlwaysOriginal)
    var qaSelectImage: UIImage! = UIImage(named: "Q&ASelected")?.imageWithRenderingMode(.AlwaysOriginal)
    var mySelectImage: UIImage! = UIImage(named: "myBagSelected")?.imageWithRenderingMode(.AlwaysOriginal)

    (tabBar.items![0] as! UITabBarItem ).selectedImage = homeSelectImage
    (tabBar.items![1] as! UITabBarItem ).selectedImage = qaSelectImage
    (tabBar.items![2] as! UITabBarItem ).selectedImage = mySelectImage

    tabBar.tintColor = UIColor.greenColor()

}
}

希望能为你效劳

答案 2 :(得分:1)

子类/扩展tabBarController并实现这些方法(我知道它的obj-c但它应该直接转换为swift):

#import "const.h"
#import "MainTabBarController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (NSArray*)tabTitles {
    return @[@"Connection",
             @"Details",
             [[NSUserDefaults standardUserDefaults] objectForKey:@"LastProfileResponse"] ? @"Profile" : @"Login",
             @"Settings"];
}

- (void)viewDidLoad {
    [super viewDidLoad];


    NSArray *imageNames = @[@"connection_tab_",
                            @"details_tab_",
                            @"profile_tab_",
                            @"settings_tab_"
                            ];

    for (int i = 0; i < self.tabBar.items.count; ++i) {
        ((UITabBarItem*)self.tabBar.items[i]).title = self.tabTitles[i];

        ((UITabBarItem*)self.tabBar.items[i]).selectedImage = [UIImage imageNamed:[imageNames[i] stringByAppendingString:@"on"]];

        ((UITabBarItem*)self.tabBar.items[i]).image = [[UIImage imageNamed:[imageNames[i] stringByAppendingString:@"off"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }

    self.tabBar.translucent = false;
    self.tabBar.barTintColor = SLATE_GREEN;
    self.tabBar.tintColor = YELLOW;
    self.delegate = self;

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:WHITE,
                                                                                                 NSForegroundColorAttributeName,
                                                                                                 TAB_FONT,
                                                                                                 NSFontAttributeName, nil]
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:YELLOW,
                                                                                                 NSForegroundColorAttributeName, nil]
                                             forState:UIControlStateSelected];
}