ios - 当我切换到另一个选项卡时,我的按钮外观会发生变化

标签 ios objective-c uitabbarcontroller

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

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

最佳答案

如果各个 View Controller 的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];

关于ios - 当我切换到另一个选项卡时,我的按钮外观会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30428451/

相关文章:

ios - 当提供商不支持自定义 URI 方案时如何重定向到 iOS

javascript - 为什么这个页面会导致 iOS 崩溃?

ios - Objective C - 编写 if 语句的更好方法

ios - 特定 UITableView 上的新 UITabbar

ios - XIB 中的 UIViewController?

ios - 打印标签时没有出现相机预览

objective-c - 如何在没有手势的情况下以编程方式触发翻页转换?

objective-c - 为什么 Apple 弃用了 dispatch_get_current_queue?

swift - 连接到 UITabBarController 的 View 不显示标签栏

选择 UITabItem 时 iOS 应用程序崩溃