在 iOS 7 中的另一个 ViewController 上显示清晰的彩色 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19486530/
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
Display clear colored ViewController over another ViewController in iOS 7
提问by daspianist
Prior to iOS 7, according to this popular Stackoverflow question,the way to show a ViewController with a clear background was to do the following in the main ViewController:
在 iOS 7 之前,根据这个流行的 Stackoverflow问题,显示具有清晰背景的 ViewController 的方法是在主 ViewController 中执行以下操作:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
However, as I have recently discovered with iOS 7 (and as commented by others to the main answer), the above solution no longer works, and instead just shows a black model controller. I know that transparency is largely used in iOS 7, so that transparent view controller is very likely possible. I haven't discovered a workaround to this issue yet, and was wondering if anyone knows how to resolve this problem. Thanks!
但是,正如我最近在 iOS 7 中发现的(以及其他人对主要答案的评论),上述解决方案不再有效,而是仅显示黑色模型控制器。我知道在 iOS 7 中主要使用透明度,因此很可能使用透明视图控制器。我还没有发现这个问题的解决方法,想知道是否有人知道如何解决这个问题。谢谢!
回答by nzs
I just followed the original solution and applied some default and custom settings in Interface Builder, and seems to me it is working.
我只是按照原始解决方案并在 Interface Builder 中应用了一些默认和自定义设置,在我看来它正在工作。
- You can check it here: https://github.com/codedad/SO_IOS_viewcl_on_viewcl
Important section (quite similar to the question's code):
重要部分(非常类似于问题的代码):
- (IBAction)click:(id)sender {
NSLog(@"click");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"TopOverVc"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
}
Also pls find the IB snaphot:
And the simulator result (after pressing button):
另外请找到IB快照:
和模拟器结果(按下按钮后):
Hope I did not misunderstand sthing in your question ;) Happy coding!
希望我没有误解你的问题;) 快乐编码!
回答by Meet
For iOS 7,subclass SecondViewController and try setting the view background color in viewDidLoad of SecondViewController
对于 iOS 7,子类 SecondViewController 并尝试在 SecondViewController 的 viewDidLoad 中设置视图背景颜色
self.view.backgroundColor = [UIColor clearColor];
回答by Satish Azad
Why not u r using this
你为什么不使用这个
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
vc.view.backgroundColor = [UIColor clearColor];
Try to change BG Color of View after view is loaded. in ur sol u r first changing View and the presenting. the View Life cycle not going on this manner
加载视图后尝试更改视图的 BG 颜色。在你的溶胶中你的第一个改变视图和呈现。视图生命周期不会以这种方式进行