iOS小贴士

2017-09-18  本文已影响8人  keisme

隐藏导航栏横线

@interface ViewController ()

@property (nonatomic, strong) UIImageView *navBarHairlineImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navBarHairlineImageView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navBarHairlineImageView.hidden = NO;
}

Storyboard勾选Hide-Bottom-Bar-on-Push带来黑影问题

去掉"Hide Bottom Bar on Push"前面的勾,在相应的控制器中使用代码来实现tabbar的显示和隐藏,即分别在viewWillAppear和viewWillDisappear方法中实现所需要的效果。

判定viewWillDisappear是push还是pop

if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
    //pop
    [self.navigationController setNavigationBarHidden:YES animated:YES]
} else {
    //push
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

自定义导航栏返回箭头导致返回手势失效

在自定义导航控制器viewDidLoad方法中加入如下代码:

self.interactivePopGestureRecognizer.delegate = (id)self;

自定义导航栏为渐变色

在自定义导航控制器中设置:

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBarBackgroundColor];
}

#pragma mark - private method

- (void)setBarBackgroundColor {
    for (UIView *view in self.navigationBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")] || [view isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
            
            NSString *phoneVersion = [[UIDevice currentDevice] systemVersion];
            UIView *iView = nil;
            if (phoneVersion.floatValue < 10.0) {
                iView = view;
            } else {
                for (UIView *subView in view.subviews) {
                    if ([subView isKindOfClass:[UIVisualEffectView class]]) {
                        iView = subView;
                    }
                }
            }
            CRLog(@"it is");
            CGRect frame = iView.frame;
            CRLog(@"%@", NSStringFromCGRect(frame));
            UIView *alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height + 20)];
            alphaView.backgroundColor = [UIColor whiteColor];
            alphaView.userInteractionEnabled = NO;
            [iView addSubview:alphaView];
            
            [self setShadeColorWithView:alphaView];
        }
    }
}

- (void)setShadeColorWithView:(UIView *)view {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.startPoint = CGPointMake(0, .5);
    gradientLayer.endPoint = CGPointMake(1, .5);
    gradientLayer.colors = @[(id)RGBA(76, 202, 90, 1).CGColor,(id)RGBA(39, 182, 112, 1).CGColor];
    gradientLayer.frame = view.bounds;
    [view.layer addSublayer:gradientLayer];
}

全局设置导航控制器返回按钮

在自定义导航控制器中做如下拦截处理:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UIImage *image = [[UIImage imageNamed:@"backButton_background"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    [super pushViewController:viewController animated:animated];
}

#pragma mark - back

- (void)back {
    [self popViewControllerAnimated:YES];
}

通过Scheme区分管理Debug和Release环境

#ifdef DEBUG
#define kServer @"http://192.168.0.1:8080"
#else
#define kServer @"http://xxoo.com"
#endif

Sample code:demo/Debug&Release-Demo

PCH常用宏定义

#define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define KSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define KSRandomColor CRColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

#define kCellSeparateColor RGBA(200, 199, 204, 1)

#ifdef DEBUG
#define KSLog(...) NSLog(@"%s %d \n %@ \n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#define kServer @"http://192.168.1.xx"
#else
#define KSLog(...) 
#define kServer @"http://xx.com"
#endif

#define kIsPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#define kFontSize12 kIsPad ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:12]
#define kFontSize14 kIsPad ? [UIFont systemFontOfSize:16] : [UIFont systemFontOfSize:14]
#define kFontSize16 kIsPad ? [UIFont systemFontOfSize:18] : [UIFont systemFontOfSize:16]
#define kFontSize18 kIsPad ? [UIFont systemFontOfSize:20] : [UIFont systemFontOfSize:18]

#define kLastWindow [[UIApplication sharedApplication].windows lastObject]
上一篇下一篇

猜你喜欢

热点阅读