iOS:ModalView与背景透明?
我想在viewController上显示一个模态视图。 (其中有一个naviguation控制器)。
在我看来,我有文字,和一个button来显示模态视图。
我创build了一个包含我的模态视图的.xib(这是一个带有图像和标签的视图)。
当我展示它时,
ShareController *controller = [[ShareController alloc] initWithNibName:@"ShareController" bundle: nil]; controller.view.backgroundColor = [UIColor clearColor]; controller.modalPresentationStyle = UIModalPresentationFormSheet; controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentViewController:controller animated:YES completion:nil];
我有我的modalview,但是,背景变成黑色..我想总是看到我的观点上的文字。 (我试图设置阿尔法,等..;但没有运行:'()
有人帮我?
谢谢,
你可以检查iOS7的例子(见我的通讯),或者你可以简单的尝试这个:
从你的“呈现”方法中删除这一行
controller.view.backgroundColor = [UIColor clearColor];
现在,在ShareController
viewDidLoad中添加:
self.view.backgroundColor = [UIColor clearColor]; self.modalPresentationStyle = UIModalPresentationCurrentContext; self.modalPresentationStyle = UIModalPresentationFormSheet;
PS
如果你有一个导航控制器…使用
[self.navigationController presentViewController:controller animated:YES completion:nil];
使用下面的代码片段在iOS 8以上:
对于Objective C:
UIViewController *walkThru = [self.storyboard instantiateViewControllerWithIdentifier:@"WalkThroughScene"]; walkThru.providesPresentationContextTransitionStyle = YES; walkThru.definesPresentationContext = YES; [walkThru setModalPresentationStyle:UIModalPresentationOverCurrentContext]; [self.navigationController presentViewController:walkThru animated:YES completion:nil];
对于Swift:
let viewController : XYZViewController = self.storyboard!.instantiateViewControllerWithIdentifier(“XYZIdentifier”) as! XYZViewController viewController.providesPresentationContextTransitionStyle = true viewController.definesPresentationContext = true viewController.modalPresentationStyle=UIModalPresentationStyle.OverCurrentContext self.presentViewController(viewController, animated: true, completion: nil)
你呈现的viewController的backgroundcolor应该是clearColor。
如果你想要你的modalVC超过标签栏,你需要定义的演示风格为UIModalPresentationOverFullScreen
[_presentedViewController setModalPresentationStyle:UIModalPresentationOverFullScreen]; [_presentingViewController presentViewController:_presentedViewController animated:YES completion:nil];
快速的答案是你不能呈现透明的模态视图,而不是presentViewController:animated:completion:方法。 因为你不能让模式视图控制器透明(你的视图是最重要的)。
你可以自定义视图,你可以手动设置它,这是一种创build你所需要的东西的方法。
在PresentingViewController
复制storyboard segue或IBAction下面的代码。
SecondViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"]; //SecondViewController *viewController = [SecondViewController alloc]init]; // if you are adding the viewcontroller programatically viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [viewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; [self presentViewController:viewController animated:YES completion:nil];
在第二个ViewController
,通过故事板或代码执行以下步骤:
- 将视图的(self.view)backgroundcolor设置为清除颜色,并将不透明度降低到50%
- 现在你可以实现一个半透明的模态视图
这就是UIWindow's
颜色,你可以在appDelegate中初始化UIWindow
颜色。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; return YES; }
Swift 3 Pankaj's答案:
let stb = UIStoryboard(name: YOUR_STORYBOARD, bundle: nil) let vc = stb.instantiateViewController(withIdentifier: YOUR_POPUP_IDENTIFIER) vc.providesPresentationContextTransitionStyle = true vc.definesPresentationContext = true vc.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext self.present(vc, animated: true, completion: nil)