我如何实时更新UITabBarController外观?

时间:2016-06-09 08:20:24

标签: ios objective-c uitabbarcontroller uiappearance

我无法通过按UIButton更新UIBarTintColor,它不起作用。这是我的层次结构:

public class GMailSender extends javax.mail.Authenticator {
...
    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
        try{
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);
            message.setDataHandler(handler);
            if (recipients.indexOf(',') > 0)
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
            Transport.send(message);
        }catch(Exception e){
            Log.d("thien.van","Network Exception");
        }
    }
}

我想通过按下ChildViewController内的按钮来实时更改UiTabbarController颜色。这是我使用的代码:

AppDelegate > HomeViewController (that holds my tabBarController) > ChildViewController

它不会实时更改我的标签栏的颜色,只有在标签栏的初始化期间使用它时才会更改颜色。我搜索并尝试了不同的解决方案:

UIColor* blu = [UIColor colorWithRed: 0.0/255 green: 161.0/255 blue: 223.0/255 alpha: 1];
[[UIView appearance] setTintColor:blu];
[[UITabBar appearance] setTintColor:blu];
[[UISlider appearance] setTintColor:blu];
[[UINavigationBar appearance] setTintColor:blu];

不适合我的情况

[self.tabBarController.tabBar setHidden:YES];
[self.tabBarController.tabBar setHidden:NO];

不适合我的情况

CALayer *layer2 =  self.tabBarController.view.layer;
[layer2 setNeedsDisplay];
[layer2 displayIfNeeded];

不适合我。

我还尝试在appDelegate中创建一个方法来包含所有这些尝试,我必须从ChildViewController尝试这个方法,而不是直接调用代码。结果是一样的。 如何通过按下按钮刷新uitabbar并更改颜色?

2 个答案:

答案 0 :(得分:0)

一旦尝试将它放在主线程上,比如

dispatch_async(dispatch_get_main_queue(), ^{
    UIColor* blu = [UIColor colorWithRed: 0.0/255 green: 161.0/255 blue: 223.0/255 alpha: 1];
    [[UIView appearance] setTintColor:blu];
    [[UITabBar appearance] setTintColor:blu];
    [[UISlider appearance] setTintColor:blu];
    [[UINavigationBar appearance] setTintColor:blu];
});

答案 1 :(得分:0)

尝试以下代码

UIColor* blu = [UIColor colorWithRed: 0.0/255 green: 161.0/255 blue: 223.0/255 alpha: 1];

[self.tabBarController.tabBar setTintColor:blu];