Semi-Modal 半模态弹出窗口

2016-05-19  本文已影响524人  卜卜星

Semi-Modal 半模态弹出窗口

主题: 怎样实现一个modal出来的view, 上半部分透明, 下班部分有View.

最终效果图

定弹出Modal的控制器为A, 被弹出的控制器为B.点击控制器A的button弹出控制器B.
思路是在B上画好下半部分不透明的view做一个覆盖层(overlay view), 然后设置B控制器的背景颜色为透明色.

    self.view.backgroundColor = [UIColor clearColor];

但是这样的话, 弹出B后, 想要B透明的部分反而变成了黑色.

查看UIViewController源码, 发现viewController有以下属性


/*
  Defines the transition style that will be used for this view controller when it is presented modally. Set
  this property on the view controller to be presented, not the presenter.  Defaults to
  UIModalTransitionStyleCoverVertical.
*/
@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle NS_AVAILABLE_IOS(3_0);
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle NS_AVAILABLE_IOS(3_2);

如官方注释所说, 要把UIModalPresentationStyle和UIModalTransitionStyle属性设置给被modal弹出的控制器, 也就是控制器B.
而这两个属性有以下详细选项:

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,
    UIModalTransitionStyleFlipHorizontal __TVOS_PROHIBITED,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED,
};

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
        UIModalPresentationFullScreen = 0,
        UIModalPresentationPageSheet NS_ENUM_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED,
        UIModalPresentationFormSheet NS_ENUM_AVAILABLE_IOS(3_2) __TVOS_PROHIBITED,
        UIModalPresentationCurrentContext NS_ENUM_AVAILABLE_IOS(3_2),
        UIModalPresentationCustom NS_ENUM_AVAILABLE_IOS(7_0),
        UIModalPresentationOverFullScreen NS_ENUM_AVAILABLE_IOS(8_0),
        UIModalPresentationOverCurrentContext NS_ENUM_AVAILABLE_IOS(8_0),
        UIModalPresentationPopover NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED,
        UIModalPresentationNone NS_ENUM_AVAILABLE_IOS(7_0) = -1,         
};

于是在A控制器modal出B控制器之前, 设置modalPresentationStyle属性给控制器B

-(void)modal:(id)sender{
    BViewController *bVC = [[BViewController alloc]init];    
    bVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;    
    [self presentViewController: bVC animated:YES completion:nil];
}

运行发现可以得到需要的效果了.
此文由这篇文章
模态(modal)一个部分透明的ViewController启发, 但是觉得作者的代码有点小问题.

考虑到兼容性的问题, iOS8之前要把弹出modal方式的属性设置给弹Modal的控制器(A控制器), 而不是被弹出的B控制器.而且要注意具体是哪个ViewController要弹出控制器.
具体的看这里

以下是一个扩展来解决兼容性问题


#define SystemVersion [UIDevice currentDevice].systemVersion.floatValue


@implementation UIViewController (Extensions)

- (void) presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
    if(SystemVersion < 8.0) {
        self.parentViewController.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
    }else{
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }

    [self presentViewController:viewControllerToPresent animated:YES completion:completion];
}

@end
上一篇下一篇

猜你喜欢

热点阅读