美文网首页#iOS#HeminWoniOS Dev IssuesObject-C
iOS 模态出一个半透明的ViewController

iOS 模态出一个半透明的ViewController

作者: Stark_Dylan | 来源:发表于2015-03-29 23:36 被阅读6078次

最近项目有需求, 需要模态初一个半透明的视图, 好多人都碰到这个问题吧, 在目标视图中设置背景颜色然后发现模态动作结束后变成了黑色或者不是半透明的颜色。

所以今天来告诉大家解决方案

- (IBAction)Avtion1:(id)sender {
    
    TestViewController * testVC = [TestViewController new];

    self.definesPresentationContext = YES; //self is presenting view controller
    testVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
    testVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    
    [self presentViewController:testVC animated:YES completion:nil];
}
  • definesPresentationContext
/*
  Determines which parent view controller's view should be presented over for presentations of type
  UIModalPresentationCurrentContext.  If no ancestor view controller has this flag set, then the presenter
  will be the root view controller.
*/
  • backgroundColor
设置你的背景颜色
  • modalPresentationStyle
/*
  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.
*/

相关文章

网友评论

  • 西叶lv:如果添加上definesPresentationContext,那么present出的VC会在导航栏的下面,不加的话,present出的VC会在导航栏的上面
    Stark_Dylan:@郝嘉律 感谢提出:blush:
  • ertyui:强,谢谢分享
    :+1: :+1: :+1:
  • Vincent_zhou:我已经找到解决方案了,谢谢你的帖子,给了我很大的帮助! :stuck_out_tongue_winking_eye:
    Stark_Dylan:@Vincent_zhou :+1::+1:
    Vincent_zhou:附上解决方案:http://www.jianshu.com/p/4f7a29bb333f
  • Vincent_zhou:你好!我底层是tabbarController,根据你的代码可以添加一个半透明的模态视图,但是好像遮挡不了底部的tabbar,我的代码是 self.tabBarController.definesPresentationContext = YES;
    请问一下,有什么解决方案吗?
  • 西楼望月:你好,UIModalPresentationOverCurrentContext是iOS8才支持的,iOS7怎么办啊?
    Stark_Dylan:@Z_Lukas http://stackoverflow.com/questions/26032472/presenting-semi-transparent-viewcontroller-that-works-both-in-ios7-and-ios8 这个帖子很好的解决了西楼望月的问题, 你看一下看能不能得到一些帮助。
    Z_Lukas:@西楼望月 解决了吗 iOS7上的问题
  • YwWyW:如果是模态一个navigationcontroller的话会出现无法push的问题。。
    YwWyW:@WildDylan 试了没,这个问题比较蛋疼
    Stark_Dylan:@YwWyW 哦? 我还没有尝试过。 明天试一下你说的。 谢谢
  • Rejected:我是你的脑残粉。。。。。。

本文标题:iOS 模态出一个半透明的ViewController

本文链接:https://www.haomeiwen.com/subject/vionxttx.html