Ios 提供一个透明的模态UIViewController
我试图自己制作一个自定义的alertView(用于iOS7+),但我很难使用alertView演示文稿 我有一个黑色背景的UIViewController(alpha设置为0.25f),还有一个alertView作为子视图 当我想显示alertView时,我会以模式显示viewController:Ios 提供一个透明的模态UIViewController,ios,uiviewcontroller,modalviewcontroller,custom-transition,Ios,Uiviewcontroller,Modalviewcontroller,Custom Transition,我试图自己制作一个自定义的alertView(用于iOS7+),但我很难使用alertView演示文稿 我有一个黑色背景的UIViewController(alpha设置为0.25f),还有一个alertView作为子视图 当我想显示alertView时,我会以模式显示viewController: -(void) show { UIWindow* window = [[UIApplication sharedApplication] keyWindow]; self.moda
-(void) show
{
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
self.modalTransitionStyle = UIModalPresentationCustom;
self.transitioningDelegate = self;
[window.rootViewController presentViewController:self animated:YES completion:nil];
}
这是我的animator对象:
-(NSTimeInterval) transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
NSLog(@"%s",__PRETTY_FUNCTION__);
return 2;
}
-(void) animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
NSLog(@"%s",__PRETTY_FUNCTION__);
UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.alpha = 0;
UIView* container = [transitionContext containerView];
[container addSubview:toView];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toView.alpha = 0.5;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
-(NSTimeInterval)transitionDuration:(id)transitionContext
{
NSLog(@“%s”,函数);
返回2;
}
-(void)animateTransfion:(id)transitionContext
{
NSLog(@“%s”,函数);
UIView*toView=[transitionContext viewForKey:UITransitionContextToViewKey];
toView.alpha=0;
UIView*容器=[transitionContext containerView];
[容器添加子视图:toView];
[UIView animateWithDuration:[self-transitionDuration:transitionContext]动画:^{
toView.alpha=0.5;
}完成:^(布尔完成){
[transitionContext completeTransition:是];
}];
}
问题是:模态VC会随着背景中的呈现VC而褪色,这是它应该做的,但是当动画结束时,呈现VC会从背景中移除
如果我调用[transitionContext CompletTransition:YES]代码>相反,演示VC在后台,但模式VC在动画结束时被删除,因此我猜如果我们发送“否”,上下文将取消演示
有没有一种方法可以使呈现的VC保持在后台,而不必对其进行快照,并将其设置为模态VC视图的背景?我认为您看到的是iOS的默认行为
当显示为模态视图控制器时,视图控制器不应是非不透明的。当动画完成时,iOS将删除参考底图视图控制器,以便在呈现的视图控制器将占据整个屏幕时加快合成速度。当视图控制器在屏幕上甚至不可见时,没有理由绘制视图控制器,因为视图控制器的视图层次结构可能很复杂
我认为你唯一的解决办法就是做一个定制的演示
备注:我没有测试这个。但事情是这样的
/* Create a window to hold the view controller. */
UIWindow *presenterWindow = [[UIWindow alloc] init];
/* Make the window transparent. */
presenterWindow.backgroundColor = [UIColor clearColor];
presenterWindow.opaque = NO;
/* Set the window rootViewController to the view controller you
want to display as a modal. */
presenterWindow.rootViewController = myModalViewController;
/* Setup animation */
CGRect windowEndFrame = [UIScreen mainScreen].bounds;
CGRect windowStartFrame = windowEndFrame;
/* Adjust the start frame to appear from the bottom of the screen. */
windowStartFrame.origin.y = windowEndFrame.size.height;
/* Set the window start frame. */
presenterWindow.frame = windowStartFrame;
/* Put the window on screen. */
[presenterWindow makeKeyAndVisible];
/* Perform the animation. */
[UIView animateWithDuration:0.5
delay:.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
presenterWindow.frame = windowEndFrame;
}
completion:^(BOOL finished){
/* Your transition end code */
}];
但是,这确实使您无法选择使用UIViewController
中内置的任何显示视图控制器逻辑。完成显示的视图控制器后,您需要自己绘制图形,然后反转动画并从屏幕中删除ui窗口。当您显示或按下它时,视图控制器不应是透明的。您可以尝试将其添加为子视图。对于过渡效果,在添加为子视图后立即更改其帧。使其帧位于可见视图之外的某个位置,然后设置动画以将帧更改为可见视图
希望这能有所帮助。有关您的信息,
我最终使我的自定义alertView成为“弹出部分”的UIView的子类。
为了显示它,我只需添加alertView作为keyWindow的子视图,并使用约束将其居中,然后在其后面放置一个透明的黑色背景视图
由于它不是控制器,我必须自己管理UI旋转(仅对于iOS 7,它与iOS 8中的UI旋转良好)。我尝试过这个解决方案,它在iOS 7和iOS 8上都能工作:
if ([[UIDevice currentDevice].systemVersion integerValue] >= 8)
{
//For iOS 8
presentingViewController.providesPresentationContextTransitionStyle = YES;
presentingViewController.definesPresentationContext = YES;
presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
//For iOS 7
presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
注意:请注意“presentingViewController”和“presentedViewController”之间的区别。我的案例可能与你的不同,但这些信息可能对对话有用
在故事板中,我将我的segue的演示改为“全屏”,它成功了 iOS8+
对于iOS8+,您可以使用以下代码段
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:secondViewController animated:YES completion:nil];
看看,更准确地说,我认为这应该是可行的。更重要的是,一开始我的alertView只是我添加到应用程序窗口的一个视图,但视图没有旋转。所以我用VC包起来帮他处理旋转。我认为您的解决方案不会处理旋转。如果我将其添加为窗口的子视图,它将不会随UI旋转,如果我将其添加为顶部VC的子视图,则透明背景不会覆盖整个UI。可能重复?VC.modalPresentationStyle=UIModalPresentationOverFullScreen;你怎么没有投票权?这是全屏体验的最佳答案(特别是从UINavigationController
演示时),使用presentedViewController.modalPresentationStyle=UIModalPresentationOverFullScreen代码>好答案。这也适用于我。我们唯一需要注意的是演示和演示视图控制器。我还必须使用alpha属性添加演示视图控制器的背景色,如:[self.view setBackgroundColor:[UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:0.4];