My button appearance change when I switch to another tab

时间:2015-05-24 21:21:10

标签: ios objective-c uitabbarcontroller

I have a problem with my back button. I'm using it only in the main tab, so it's not displaying in the other tabs. When The view load the first time, my button appears the way I want. But when I switch to another tab and go back to the main tab, the button is bordered in a black color. Here is what I'm doing:

First time loaded

when you change tab and go back this is how the button looks like

    #import "FirstViewController.h"

@interface FirstViewController ()
@property UIActivityIndicatorView *indicator;
@property bool tabHidden;
@end

@implementation FirstViewController

static UIWebView* static_iVar = nil;


- (void)viewDidLoad {
    [super viewDidLoad];


    float width = [UIScreen mainScreen].bounds.size.width;

    float height = [UIScreen mainScreen].bounds.size.height;

    static_iVar = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];

    NSURL *url = [NSURL URLWithString:@"http://guiasdelsur.es"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [static_iVar loadRequest:requestObj];
    [self.view addSubview:static_iVar];

    static_iVar.delegate = self;


    UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
    swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;

    UISwipeGestureRecognizer* swipeUpGestureRecognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDownFrom:)];
    swipeUpGestureRecognizer2.direction = UISwipeGestureRecognizerDirectionDown;
    swipeUpGestureRecognizer.delegate = self;
    swipeUpGestureRecognizer2.delegate = self;

    [static_iVar addGestureRecognizer:swipeUpGestureRecognizer];
    [static_iVar addGestureRecognizer:swipeUpGestureRecognizer2];



    //first tab

    _tabInicio = [[UITabBarItem alloc] initWithTitle:@"Inicio" image:[UIImage imageNamed:@"d.png"] tag:0];

    [[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
    [self.tabBarController setSelectedIndex:0];
    self.tabBarItem = _tabInicio;


    [[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:@"programas.png"]];
    [[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:@"actividades.png"]];
    [[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:@"nosotros.png"]];



    // Do any additional setup after loading the view, typically from a nib.

    _cToolBar = [[UIToolbar alloc] init];
    _cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    NSMutableArray *items = [[NSMutableArray alloc] init];


    NSString *backArrowString = @"\U000025C0\U0000FE0E Atrás"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTOR
    _back = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:nil action:nil];


    [_cToolBar setBarStyle:UIBarStyleBlack];
    [_cToolBar setTranslucent:YES ];
    [_cToolBar setTintColor:[UIColor greenColor]];


    _right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:@selector(refreshControl)];
    [_right setTintColor:[UIColor greenColor]];

    [items addObject:_back];
    [items addObject:_flexibleSpace];
    [items addObject:_right];



    [_cToolBar setItems:items animated:NO];
    [self.view addSubview:_cToolBar];



    //loading indicator
    _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [_indicator setCenter:self.view.center];
    [_indicator setHidesWhenStopped:YES];
    [self.view addSubview:_indicator];
    [_indicator startAnimating];



}

-(void)doesNothing{

}
-(void)goBack{
    [static_iVar goBack];
}
-(void)refreshControl{
    [super viewDidAppear:YES];
    [static_iVar reload];
    [_indicator startAnimating];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
+(void)viewController:(NSString*)link{
    NSURL *url = [NSURL URLWithString:link];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [static_iVar loadRequest:requestObj];

}

- (void)webViewDidStartLoad:(UIWebView *)webView
{

    [_indicator startAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_indicator stopAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    [_indicator stopAnimating];
    CGSize contentSize = webView.scrollView.contentSize;
    CGSize viewSize = self.view.bounds.size;

    float rw = viewSize.width / contentSize.width;

    webView.scrollView.minimumZoomScale = rw;
    webView.scrollView.maximumZoomScale = rw;
    webView.scrollView.zoomScale = rw;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    return TRUE;

}
- (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer {


    if(_tabHidden == YES){
        [self showTabBar:self.tabBarController];
        _tabHidden = NO;
    }


}
- (void)handleSwipeDownFrom:(UIGestureRecognizer*)recognizer {

    if(_tabHidden == NO){
        [self hideTabBar:self.tabBarController];
        _tabHidden = YES ;
    }

}


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

    return YES;
}

- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width;
    }

    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
    [UIView commitAnimations];
}



- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float fHeight = screenRect.size.height - tabbarcontroller.tabBar.frame.size.height;

    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width - tabbarcontroller.tabBar.frame.size.height;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
        }
    }
    [UIView commitAnimations];
}

@end

2 个答案:

答案 0 :(得分:1)

Do not set them inside if the viewDidLoad for the individual view controllers. Set them when you instantiate the UITabBarController like so:

//Perform setup
...

//Set tab bar items
moreVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"More"
                                                    image:[UIImage imageNamed:@"More"]
                                            selectedImage:[UIImage imageNamed:@"More"]];

tabBarCon.viewControllers = @[vc1, vc2, vc3, moreVC];

答案 1 :(得分:0)

你的问题在于hideTabBar()方法:

- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width;
    }

    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
    [UIView commitAnimations];
}

您正在将tabbarcontroller的子视图的backgroundColor更改为blackColor。