Skip to content

Commit

Permalink
解决来电话导航栏偏移问题
Browse files Browse the repository at this point in the history
  • Loading branch information
chenliangloveyou committed Jan 11, 2018
1 parent d76272c commit 7b93448
Show file tree
Hide file tree
Showing 26 changed files with 659 additions and 432 deletions.
15 changes: 15 additions & 0 deletions EasyNavigation/EasyNavigationButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// EasyNavigationButton.h
// EFDoctorHealth
//
// Created by nf on 2017/12/28.
// Copyright © 2017年 NF. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface EasyNavigationButton : UIButton

+ (instancetype)buttonWithTitle:(NSString *)title image:(UIImage *)image ;

@end
105 changes: 105 additions & 0 deletions EasyNavigation/EasyNavigationButton.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// EasyNavigationButton.m
// EFDoctorHealth
//
// Created by nf on 2017/12/28.
// Copyright © 2017年 NF. All rights reserved.
//

#import "EasyNavigationButton.h"
#import "EasyNavigationOptions.h"

#define kButtonInsetsH 10.0f //按钮上下图文距按钮边缘的距离
#define kButtonInsetsW 5.0f //按钮左右局边缘的距离

@interface EasyNavigationButton()

@property (nonatomic,strong)NSString *title ;
@property (nonatomic,strong)UIImage *image;

@end

@implementation EasyNavigationButton


+ (instancetype)buttonWithTitle:(NSString *)title image:(UIImage *)image
{
EasyNavigationButton *button = [super buttonWithType:UIButtonTypeCustom] ;

if (button) {

button.title = title ;
button.image = image ;
EasyNavigationOptions *options = [EasyNavigationOptions shareInstance];

CGFloat buttonW = kButtonInsetsW*2 ;
if (image) {
CGFloat imageHeight = NavigationNorlmalHeight_N()-2*kButtonInsetsH ;
buttonW += (imageHeight) ;
}
if (!ISEMPTY_N(title)) {
CGFloat titleW = [title sizeWithAttributes:@{NSFontAttributeName: options.buttonTitleFont}].width ;
buttonW += titleW ;
}
if (image && !ISEMPTY_N(title)) {
buttonW += kButtonInsetsW ;
}
[button setFrame:CGRectMake(0, 0, buttonW, NavigationNorlmalHeight_N())];


if (!ISEMPTY_N(title)) {
[button setTitle:title forState:UIControlStateNormal];
}
if (image) {
[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFit ;//imageview需要放到中间
}

[button setTitleColor:options.buttonTitleColor forState:UIControlStateNormal];
[button setTitleColor:options.buttonTitleColorHieght forState:UIControlStateHighlighted];
[button.titleLabel setFont:options.buttonTitleFont] ;

// button.titleLabel.backgroundColor = [UIColor cyanColor];
// button.imageView.backgroundColor = [UIColor blueColor];
// [button setBackgroundColor:kColorRandom];
};

return button ;
}


- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
if (_image) {
CGFloat imageX = kButtonInsetsW ;
CGFloat imageY = kButtonInsetsH ;
CGFloat imageWH = NavigationNorlmalHeight_N() - 2*kButtonInsetsH ;
return CGRectMake(imageX, imageY, imageWH, imageWH);
}
else{
return CGRectZero ;
}
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
if (ISEMPTY_N(_title)) {
return CGRectZero ;
}

CGFloat x = kButtonInsetsW ;
CGFloat w = CGRectGetWidth(contentRect) - 2*kButtonInsetsW ;
if (_image){
x = 2*kButtonInsetsW+ NavigationNorlmalHeight_N() - 2*kButtonInsetsH ;
w = CGRectGetWidth(contentRect) - (3*kButtonInsetsW+NavigationNorlmalHeight_N() - 2*kButtonInsetsH ) ;
}

CGFloat y = kButtonInsetsH ;
CGFloat h = NavigationNorlmalHeight_N() - 2*kButtonInsetsH ;
return CGRectMake(x, y, w, h) ;

}



@end
2 changes: 1 addition & 1 deletion EasyNavigation/EasyNavigationController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@interface EasyNavigationController : UINavigationController<UIGestureRecognizerDelegate>

//@property (nonatomic,assign)BOOL isSystemNavigationBar ;//是否展示自带的导航条
@property (nonatomic,assign)BOOL isSystemNavigationBar ;//是否展示自带的导航条

@property (nonatomic,strong,readonly)UIPanGestureRecognizer *customBackGesture ;//自定义侧滑返回

Expand Down
101 changes: 62 additions & 39 deletions EasyNavigation/EasyNavigationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ @implementation EasyNavigationController

- (void)dealloc
{
EasyNotificationRemove(UIApplicationWillChangeStatusBarFrameNotification) ;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
[[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}

Expand All @@ -42,33 +42,54 @@ - (void)viewDidLoad
self.navigationBarHidden = NO ;
self.navigationBar.hidden = YES ;
self.delegate = self ;

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
EasyNotificationAdd(statusBarChangeNoti:, UIApplicationDidChangeStatusBarFrameNotification) ;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChangeNoti:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{

}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
EasyNavigationView *navView = self.topViewController.navigationView ;
if (navView.width != self.topViewController.view.width) {
navView.width = self.topViewController.view.width ;
}
});
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// EasyNavigationView *navView = self.topViewController.navigationView ;
// if (navView.width != self.topViewController.view.width) {
// navView.width = self.topViewController.view.width ;
// }
// if (self.view.width != SCREEN_WIDTH) {
// navView.centerX = self.view.centerX ;
// }
// });

// 移除全屏滑动手势
if ([self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.systemGestureTarget]) {
[self.interactivePopGestureRecognizer.view removeGestureRecognizer:self.systemGestureTarget];
}

//重新处理手势
[viewController dealSlidingGestureDelegate];

}
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{

}

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated
{
UIViewController *lastVC = viewControllers.lastObject ;
NSAssert(lastVC, @"the viewcontrollers array is empty !");

if (self.viewControllers.count > 0 ) {
lastVC.hidesBottomBarWhenPushed = YES;
}

[super setViewControllers:viewControllers animated:YES];

}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
Expand All @@ -77,14 +98,11 @@ - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)ani
}

[super pushViewController:viewController animated:animated];

if (self.viewControllers.count > 1) {
kWeakSelf(self)
UIImage *img = [UIImage imageNamed:EasyImageFile(@"nav_btn_back.png")] ;
UIButton *backButton = [viewController.navigationView addLeftButtonWithTitle:@" " image:img clickCallBack:^(UIView *view) {
[weakself popViewControllerAnimated:YES];
}];
viewController.navigationView.backButton = backButton ;

if (IsIphoneX_N()) { // 修改tabBra的frame
CGRect frame = self.tabBarController.tabBar.frame;
frame.origin.y = ScreenHeight_N() - frame.size.height;
self.tabBarController.tabBar.frame = frame;
}

}
Expand All @@ -104,38 +122,43 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
}
return YES;
}

- (void)setTitle:(NSString *)title
{
if (self.topViewController.navigationView) {
[self.navigationView setTitle:title];
}
}
- (void)statusBarChangeNoti:(NSNotification *)notifycation
{

[self setNeedsStatusBarAppearanceUpdate];

EasyNavigationView *navView = self.topViewController.navigationView ;
if (!navView) return ;

if (navView.width != self.topViewController.view.width) {
navView.width = self.topViewController.view.width ;
}

[navView layoutNavSubViews];

UIDevice *device = [UIDevice currentDevice] ;

if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {
NSLog(@"竖屏 ====== %f , %f",self.topViewController.view.width ,self.topViewController.navigationView.height);
EasyLog_N(@"竖屏 ====== %f , %f",self.topViewController.view.width ,self.topViewController.navigationView.height);
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
NSLog(@"横屏====== %f , %f",self.topViewController.view.width ,self.topViewController.navigationView.height);
EasyLog_N(@"横屏====== %f , %f",self.topViewController.view.width ,self.topViewController.navigationView.height);
}
else{
NSLog(@"未知状态====== %zd",device.orientation );
EasyLog_N(@"未知状态====== %zd",device.orientation );
}

}

- (UIStatusBarStyle)preferredStatusBarStyle
{
return !self.topViewController.statusBarStyle ;
return self.topViewController.statusBarStyle ;
}


Expand Down Expand Up @@ -171,13 +194,13 @@ - (void)didReceiveMemoryWarning {
}

/*
#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 - 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.
}
*/

@end
6 changes: 5 additions & 1 deletion EasyNavigation/EasyNavigationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@
*/
@property (nonatomic,strong)UIColor *buttonTitleColorHieght ;


/**
* 系统返回按钮 图片/文字 更改
*/
@property (nonatomic,strong)UIImage *navigationBackButtonImage ;
@property (nonatomic,strong)NSString *navigationBackButtonTitle ;
@end
6 changes: 3 additions & 3 deletions EasyNavigation/EasyNavigationOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (instancetype)init
_navbigTitleType = NavBigTitleTypeDefault ;
_navTitleAnimationType = NavTitleAnimationTypeStiffFade ;

_backGroundAlpha = 0.9 ;
_backGroundAlpha = 1.0f ;

_navBackGroundColor = [UIColor whiteColor];
_navLineColor = [UIColor groupTableViewBackgroundColor];
Expand All @@ -54,8 +54,8 @@ - (instancetype)init

_buttonTitleFont = [UIFont systemFontOfSize:16];
_buttonTitleColor = [UIColor blueColor] ;
_buttonTitleColorHieght = [UIColor darkTextColor];
_buttonTitleColorHieght = [[UIColor blueColor] colorWithAlphaComponent:0.3];

}
return self ;
}
Expand Down
Loading

0 comments on commit 7b93448

Please sign in to comment.