iOS:背景透明的ModalView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21760698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS : ModalView with background transparent?
提问by deveLost
I want to show a modalview on a viewController. (which has a naviguation controller).
我想在 viewController 上显示一个模态视图。(它有一个导航控制器)。
On my view i have text, and a button to show the modalview.
在我的视图中,我有文本和一个显示模态视图的按钮。
I created a .xib which contained my modalview (it's a view with an image and a label).
我创建了一个 .xib,其中包含我的 modalview(它是一个带有图像和标签的视图)。
When i show it, with that :
当我展示它时:
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];
i have my modalview, BUT, the background become black.. and i want to see always the text on my view. (i tried to set alpha, etc..; but NOTHING runs :'( )
我有我的模态视图,但是,背景变成黑色..我想总是看到我视图上的文本。(我试图设置 alpha 等。但没有任何东西运行 :'( )
Someone to help me ?
有人来帮助我吗?
Thanks,
谢谢,
采纳答案by TonyMkenu
you can check the iOS7 example (see my comm) or you can simple try this:
您可以查看 iOS7 示例(请参阅我的通讯),或者您可以简单地尝试以下操作:
remove this line from your "presenting" method
从您的“呈现”方法中删除此行
controller.view.backgroundColor = [UIColor clearColor];
now, in viewDidLoad of the ShareController
add:
现在,在 viewDidLoad 中ShareController
添加:
self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
self.modalPresentationStyle = UIModalPresentationFormSheet;
PS
聚苯乙烯
if you have a navigationController... use
如果你有一个导航控制器......使用
[self.navigationController presentViewController:controller animated:YES completion:nil];
回答by pankaj
Use following snippet to do it on iOS 8 onwards:
使用以下代码段在 iOS 8 上执行此操作:
For Objective C:
对于目标 C:
UIViewController *walkThru = [self.storyboard instantiateViewControllerWithIdentifier:@"WalkThroughScene"];
walkThru.providesPresentationContextTransitionStyle = YES;
walkThru.definesPresentationContext = YES;
[walkThru setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self.navigationController presentViewController:walkThru animated:YES completion:nil];
For Swift 2 :
对于 Swift 2 :
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)
For Swift 4 :
对于 Swift 4 :
let viewController = self.storyboard!.instantiateViewController(withIdentifier: "XYZIdentifier") as! XYZViewController
viewController.providesPresentationContextTransitionStyle = true
viewController.definesPresentationContext = true
viewController.modalPresentationStyle = .overCurrentContext
self.present(viewController, animated: true, completion: nil)
And the backgroundcolor of your presented viewController should be clearColor.
并且您呈现的 viewController 的背景颜色应该是 clearColor。
回答by dorian
If you want your modalVC to be over the tabbar you need to define the presentation style as UIModalPresentationOverFullScreen
如果您希望 modalVC 位于选项卡栏上方,则需要将演示样式定义为 UIModalPresentationOverFullScreen
[_presentedViewController setModalPresentationStyle:UIModalPresentationOverFullScreen];
[_presentingViewController presentViewController:_presentedViewController animated:YES completion:nil];
回答by Greg
The quick answer is you cannot present transparent modal view, not with presentViewController:animated:completion: method. Because you cannot make the modal view controller transparent (your view is placer on top of that).
快速回答是您不能呈现透明的模态视图,而不是使用 presentViewController:animated:completion: 方法。因为您不能使模态视图控制器透明(您的视图位于其之上)。
You can make custom view and you can manually animate it, this is a way to create something what you need.
您可以制作自定义视图,也可以手动为其设置动画,这是一种创建所需内容的方法。
回答by vigneshwaran s
In the PresentingViewController
copy the below code under storyboard segue or IBAction.
在PresentingViewController
故事板 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];
In the second ViewController
do the following steps either by storyboard or through code:
在第二个中ViewController
,通过情节提要或通过代码执行以下步骤:
- Set the view's (
self.view
) backgroundcolor to clearcolor and reduce the opacity to 50 % - Now you can realize a semi transparent modal view
- 将视图的 (
self.view
) 背景色设置为 clearcolor 并将不透明度降低到 50% - 现在你可以实现一个半透明的模态视图
回答by N.nnd
That is the UIWindow's
color,you can init the UIWindow
color in appDelegate.
那就是UIWindow's
颜色,你可以UIWindow
在 appDelegate 中初始化颜色。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}