【问题标题】:My button appearance change when I switch to another tab当我切换到另一个选项卡时,我的按钮外观发生了变化
【发布时间】:2015-05-24 21:21:10
【问题描述】:

我的后退按钮有问题。我只在主选项卡中使用它,所以它没有显示在其他选项卡中。当视图第一次加载时,我的按钮会以我想要的方式出现。但是当我切换到另一个选项卡并返回主选项卡时,该按钮的边框为黑色。这是我正在做的事情:

    #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

【问题讨论】:

    标签: ios objective-c uitabbarcontroller


    【解决方案1】:

    如果viewDidLoad 用于单个视图控制器,请不要将它们设置在内部。在实例化 UITabBarController 时设置它们,如下所示:

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

    【讨论】:

    • 我用故事板做的,我应该从故事板上删除它吗?
    【解决方案2】:

    您的问题在于 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。

    【讨论】:

    • 在问题已经出现在我的应用程序中之后,我添加了此方法。我还注释了那行代码以防万一,但问题仍然存在..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2019-05-03
    • 2018-07-03
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多