swift学习UI

iOS导航栏、状态栏及Tabbar高度(区分iPhone X与其

2020-06-09  本文已影响0人  文子飞_
#define kIs_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIs_iPhoneX kScreenWidth >=375.0f && kScreenHeight >=812.0f&& kIs_iphone
 
/*状态栏高度*/
#define kStatusBarHeight (CGFloat)(kIs_iPhoneX?(44.0):(20.0))
/*导航栏高度*/
#define kNavBarHeight (44)
/*状态栏和导航栏总高度*/
#define kNavBarAndStatusBarHeight (CGFloat)(kIs_iPhoneX?(88.0):(64.0))
/*TabBar高度*/
#define kTabBarHeight (CGFloat)(kIs_iPhoneX?(49.0 + 34.0):(49.0))
/*顶部安全区域远离高度*/
#define kTopBarSafeHeight (CGFloat)(kIs_iPhoneX?(44.0):(0))
/*底部安全区域远离高度*/
#define kBottomSafeHeight (CGFloat)(kIs_iPhoneX?(34.0):(0))
/*iPhoneX的状态栏高度差值*/
#define kTopBarDifHeight (CGFloat)(kIs_iPhoneX?(24.0):(0))
/*导航条和Tabbar总高度*/
#define kNavAndTabHeight (kNavBarAndStatusBarHeight + kTabBarHeight)
————————————————
版权声明:本文为CSDN博主「云水天川-个人软件开发者」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hangapple/article/details/80906291
BaseNavigationVC 1、隐藏/显示navigationBar 2、自定义导航之后,侧滑返回失效
@interface BaseNavigationController()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation BaseNavigationController
- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;    
    // 默认开启系统右划返回
    self.interactivePopGestureRecognizer.enabled = YES;
    self.interactivePopGestureRecognizer.delegate = self;
}

#pragma mark - iOS 平滑隐藏/显示navigationBar
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([viewController isKindOfClass:[BaseViewController class]]) {
        WSFBaseViewController * vc = (BaseViewController *)viewController;
        if (vc.hidenNaviBar) {
            vc.view.top = 0;
            [vc.navigationController setNavigationBarHidden:YES animated:animated];
        } else {
            vc.view.top = kTopHeight;
            [vc.navigationController setNavigationBarHidden:NO animated:animated];
        }
    }
}

// iOS 自定义导航之后,侧滑返回失效
#pragma mark - 根视图禁用右划返回
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    if (self.childViewControllers.count==1) {
        return NO;
    }
    else if(self.viewControllers.count==2)
    {
        if ([self.viewControllers.firstObject isMemberOfClass:[BaseViewController class]]) {
            return NO;
        }
        else
            return YES;
    }
    return  YES;
}

#pragma mark - 统一app状态栏样式
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}
@end

BaseVC
@interface BaseVC()
/// 修改状态栏颜色
@property (nonatomic, assign) UIStatusBarStyle StatusBarStyle;
/// 是否隐藏导航栏
@property (nonatomic, assign) BOOL hidenNaviBar;
@end

@implementation BaseVC

#pragma mark - 创建子视图
- (void)createBackButton {
    
    // 返回按钮
    if (self.navigationController.viewControllers.count > 1) {
        // 返回按钮
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, YCFitScreen(60), YCFitScreen(60));
        [backButton setImage:[UIImage imageNamed:@"page_return_black"] forState:UIControlStateNormal];
        backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -YCFitScreen(20), 0, 0);
        //[backButton setBackgroundImage:[UIImage imageNamed:@"page_return_black"] forState:UIControlStateNormal];
        //[backButton setImage:[UIImage imageNamed:@"icon_product_return"] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(backBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.backButton = backButton;
        [self.view addSubview:backButton];
        UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = leftBarButton;
    }
    
}
#pragma mark - buttonAction:
- (void)backBarButtonAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 设置状态栏样式
- (UIStatusBarStyle)preferredStatusBarStyle{
    return _StatusBarStyle;
}
//动态更新状态栏颜色
-(void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle{
    _StatusBarStyle=StatusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}

@end

上一篇下一篇

猜你喜欢

热点阅读