CNLiveBaseViewController.m 7.9 KB
//
//  CNLiveBaseViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/17.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveBaseViewController.h"

@interface CNLiveBaseViewController ()
@property (strong, nonatomic, readwrite) AppDelegate *appDelegate;
@property (weak, nonatomic) UIButton *headerButton;

@end

@implementation CNLiveBaseViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationMaskPortrait];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

//    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    
    [self setUpNavigationBar];
    self.view.backgroundColor = [UIColor whiteColor];
//    [self.navigationController setNavigationBarHidden:NO];

}

- (void)setName:(NSString *)name{
    _name = name;
    [_headerButton setTitle:name forState:UIControlStateNormal];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView.appearance.estimatedRowHeight = 0;
    UITableView.appearance.estimatedSectionFooterHeight = 0;
    UITableView.appearance.estimatedSectionHeaderHeight = 0;
    // Do any additional setup after loading the view.
    
}
#pragma mark - UI
- (void)setUpNavigationBar{
    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:18]};

    //左
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_icon"] forState:UIControlStateNormal];
    [headerButton setImage:[UIImage imageNamed:@"cnlive_icon"] forState:UIControlStateHighlighted];
    [headerButton setImage:[UIImage imageNamed:@"cnlive_icon"] forState:UIControlStateSelected];

    [headerButton setTitle:_name forState:UIControlStateNormal];
    if (HOMETITLECOLORSTR) {
        [headerButton setTitleColor:[UIColor colorWithHexString:HOMETITLECOLORSTR] forState:UIControlStateNormal];
    }else{
    [headerButton setTitleColor:Color_Blue forState:UIControlStateNormal];
    }

    headerButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    headerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//居左显示
    headerButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    _headerButton = headerButton;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
    
    //右
    UIButton *languageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 44)];
    if(IS_CHINESE){
        [languageButton setImage:[UIImage imageNamed:@"cnlive_EN"] forState:UIControlStateNormal];
        [languageButton setImage:[UIImage imageNamed:@"cnlive_CN"] forState:UIControlStateSelected];
    }else{
        [languageButton setImage:[UIImage imageNamed:@"cnlive_CN"] forState:UIControlStateNormal];
        [languageButton setImage:[UIImage imageNamed:@"cnlive_EN"] forState:UIControlStateSelected];
    }
    
    [languageButton addTarget:self action:@selector(chinaENLanguageSwitch:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:languageButton];
    
    self.navigationItem.rightBarButtonItem = item2;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Action
//中英文切换
- (void)chinaENLanguageSwitch:(UIButton *)btn{
    NSString *title = IS_CHINESE?@"Switch To English?":@"切换到中文版本?";
    NSString *subTitle = IS_CHINESE?@"切换到英文版本?":@"Switch To Chinese?";

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"切换 Switch" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定,进入确定的事件");
        btn.selected = !btn.selected;
        
        self->_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        //本地存储语言版本
        if (IS_CHINESE) {
            [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
            [User_Defaults setObject:@"en" forKey:Language_Local];
            self->_appDelegate.lan = @"en";
            [User_Defaults synchronize];

        }else{
            [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", nil] forKey:@"AppleLanguages"];
            [User_Defaults setObject:@"zh-Hans" forKey:Language_Local];
            self->_appDelegate.lan =@"zh-Hans";
            [User_Defaults synchronize];
            
        }

        [(AppDelegate *)[UIApplication sharedApplication].delegate initializeSDKConfiguration];
//        if([[ZXWUserInformation shareZXWUserInformation] isLogin]){
//            [[ZXWUserInformation shareZXWUserInformation] loginOut];
//            // 取消第三方平台授权
//            [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];
//            [ShareSDK cancelAuthorize:SSDKPlatformTypeQQ];
//            [ShareSDK cancelAuthorize:SSDKPlatformTypeSinaWeibo];
//        }
        
        //切换RootViewController
        if(IS_CHINESE){
            CNLiveTabBarController *liveTab = [[CNLiveTabBarController alloc] init];
            XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:liveTab];
            //设置左右菜单
            slideMenu.slideEnabled = NO;
            CNHomeRightViewController *rightVC = [[CNHomeRightViewController alloc]init];
            rightVC.block = ^(NSInteger row) {
                CNHomeViewController *vc = (CNHomeViewController *)liveTab.viewControllers[0];
                vc.segmentControl.current = row;
            };
            slideMenu.rightViewController = rightVC;
            [UIApplication sharedApplication].keyWindow.rootViewController = slideMenu;
            
        }else{
            CNLiveENTabBarController *liveTab = [[CNLiveENTabBarController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = liveTab;
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguageVersion" object:nil];
        
    }];

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消 Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
    
}
- (void)goBack {
    
    if(self.navigationController.viewControllers.count > 0){
        [self.navigationController popViewControllerAnimated:YES];

    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}

- (BOOL)shouldAutorotate {
    return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
    
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end