實(shí)現(xiàn)iPad和iPhone的VC差別顯示

我們要實(shí)現(xiàn)同一個(gè)ViewController,在iPad和iPhone上顯示效果不一樣
iPad上時(shí)popover的,可以自定義其大小,iPhone上是全屏顯示。推入方式為present

如圖:

iPad

iPhone

在PresentingViewController(也就是基礎(chǔ)頁面)中添加代碼,用它來彈出PresentedViewController

//入口方法
- (IBAction)btnAction:(id)sender {
    
    NextVC* vc = [[NextVC alloc] init];
    vc.title = @"Blood Pressure";
    vc.previousVC = self;
    [self popOverToViewController:vc];
}


- (CGRect)centerRectForSize:(CGSize)forSize inSize:(CGSize)inSize{
    CGFloat x = (inSize.width - forSize.width)/2;
    CGFloat y = (inSize.height - forSize.height)/2;
    CGFloat width = forSize.width;
    CGFloat height = forSize.height;
    return CGRectMake(x, y, width, height);
}


- (void)popOverToViewController:(UIViewController*)viewController{
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    if (!kIsIPAD) {
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    viewController.preferredContentSize = CGSizeMake(640, 460);
    nav.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController* poC = [nav popoverPresentationController];
    //    poC.barButtonItem = [self.navigationItem leftBarButtonItem];
    poC.sourceView = self.view;
    poC.permittedArrowDirections = 0;
    poC.delegate = self;
    poC.backgroundColor = [UIColor grayColor];
    poC.sourceRect = [self centerRectForSize:CGSizeMake(640, 460) inSize:self.view.bounds.size];
    [self presentViewController:nav animated:YES completion:^{
        poC.containerView.layer.shadowOffset = CGSizeMake(4, 4);
        poC.containerView.layer.shadowOpacity = 0.25;
        poC.containerView.layer.shadowRadius = 6;
//        poC.containerView.layer.shadowColor = [UIColor blackColor].CGColor;
//        poC.containerView.layer.masksToBounds = NO;
    }];
}

#pragma mark - popover delegate

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController{
    [self addBlurEffect:YES];
//    self.navigationController.view.alpha = 0.2;
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    return NO;
}

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
//    self.navigationController.view.alpha = 1;
    [self addBlurEffect:NO];
}

- (void)addBlurEffect:(BOOL)add{
    if (add) {
        //  創(chuàng)建需要的毛玻璃特效類型
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        //  毛玻璃view 視圖
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        //添加到要有毛玻璃特效的控件中
        effectView.frame = self.navigationController.view.bounds;
        [self.navigationController.view addSubview:effectView];
        _blurView = effectView;
        self.navigationController.view.alpha = 0.96;
        [self.navigationController.view.layer setShouldRasterize:YES];
    }else{
        [_blurView removeFromSuperview];
        self.navigationController.view.alpha = 1;
        [self.navigationController.view.layer setShouldRasterize:NO];
    }
}

除此之外,還需要在推入的界面(PresentedViewController)消失的時(shí)候,添加代碼,再去掉上一個(gè)頁面(PresentingViewController)的Blur效果:

if (_previousVC) {
        [_previousVC addBlurEffect:NO];
    }
    [self.navigationController dismissViewControllerAnimated:NO completion:nil];

關(guān)于PresentingVC和PresentedVC,可以參考下圖:

PresentingVC和PresentedVC

也可以參考文章:
http://www.15yan.com/story/jlkJnPmVGzc/

=========分割線=============

這樣可以顯示,但是代碼不方便管理,于是在我們的項(xiàng)目里,把這個(gè)實(shí)現(xiàn)做成了分類,代碼如下:

UIViewController+Popover.h:

//
//  UIViewController+Popover.h
//  iHealthLayeredApp
//
//  Created by Realank on 2017/4/21.
//  Copyright ? 2017年 Realank. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIViewController (Popover)<UIPopoverPresentationControllerDelegate>

- (void)popOverToViewController:(UIViewController*)viewController;
- (void)removeBlurEffect;
@end

UIViewController+Popover.m:

//
//  UIViewController+Popover.m
//  iHealthLayeredApp
//
//  Created by Realank on 2017/4/21.
//  Copyright ? 2017年 Realank. All rights reserved.
//

#import "UIViewController+Popover.h"

#import <objc/runtime.h>

@interface UIBlurEffect (Protected)
@property (nonatomic, readonly) id effectSettings;
@end

@interface MyBlurEffect : UIBlurEffect
@end

@implementation MyBlurEffect

+ (instancetype)effectWithStyle:(UIBlurEffectStyle)style
{
    id result = [super effectWithStyle:style];
    object_setClass(result, self);
    
    return result;
}

- (id)effectSettings
{
    id settings = [super effectSettings];
    [settings setValue:@6 forKey:@"blurRadius"];
    return settings;
}

- (id)copyWithZone:(NSZone*)zone
{
    id result = [super copyWithZone:zone];
    object_setClass(result, [self class]);
    return result;
}

@end


@implementation UIViewController (Popover)

- (CGRect)centerRectForSize:(CGSize)forSize inSize:(CGSize)inSize{
    CGFloat x = (inSize.width - forSize.width)/2;
    CGFloat y = (inSize.height - forSize.height)/2;
    CGFloat width = forSize.width;
    CGFloat height = forSize.height;
    return CGRectMake(x, y, width, height);
}

- (UIView*)backgroundView{
    if (self.navigationController) {
        return self.navigationController.view;
    }
    if ([self isKindOfClass:[UITableViewController class]]) {
        UITableViewController* vc = (UITableViewController*)self;
        return vc.tableView;
    }else if ([self isKindOfClass:[UICollectionView class]]) {
        UICollectionView* vc = (UICollectionView*)self;
        return vc.backgroundView;
    }else{
        return self.view;
    }
}


- (void)popOverToViewController:(UIViewController*)viewController{
    
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    if (!kIsIPAD) {
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    viewController.preferredContentSize = CGSizeMake(640, 460);
    nav.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController* poC = [nav popoverPresentationController];
    //    poC.barButtonItem = [self.navigationItem leftBarButtonItem];
    poC.sourceView = [self backgroundView];
    poC.permittedArrowDirections = 0;
    poC.delegate = self;
    poC.backgroundColor = [UIColor grayColor];
    poC.sourceRect = [self centerRectForSize:CGSizeMake(640, 460) inSize:[self backgroundView].bounds.size];
    [self presentViewController:nav animated:YES completion:^{
        poC.containerView.layer.shadowOffset = CGSizeMake(4, 4);
        poC.containerView.layer.shadowOpacity = 0.25;
        poC.containerView.layer.shadowRadius = 6;
        //        poC.containerView.layer.shadowColor = [UIColor blackColor].CGColor;
        //        poC.containerView.layer.masksToBounds = NO;
    }];
}

#pragma mark - popover delegate

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController{
    [self addBlurEffect:YES];
    //    self.navigationController.view.alpha = 0.2;
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    return NO;
}

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    //    self.navigationController.view.alpha = 1;
    [self addBlurEffect:NO];
}

- (void)addBlurEffect:(BOOL)add{
    static UIVisualEffectView *blurView = nil;
    if (add) {
        //  創(chuàng)建需要的毛玻璃特效類型
        MyBlurEffect *blurEffect = [MyBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        //  毛玻璃view 視圖
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        //添加到要有毛玻璃特效的控件中
        effectView.frame = self.navigationController.view.bounds;
        [[self backgroundView] addSubview:effectView];
        blurView = effectView;
        effectView.alpha = 0.8;
//        self.navigationController.view.alpha = 0.96;
        [[self backgroundView].layer setShouldRasterize:YES];
    }else{
        [blurView removeFromSuperview];
        blurView = nil;
//        self.navigationController.view.alpha = 1;
        [[self backgroundView].layer setShouldRasterize:NO];
    }
}

- (void)removeBlurEffect{
    [self addBlurEffect:NO];
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容