Zia*_*hen 27 iphone xcode uiview uiwindow uialertview
为了使UIView
所有视图都像行为一样UIAlertView
,我看到的最好的方法是将它放在应用程序窗口中,即将视图添加到:
[[[UIApplication sharedApplication] delegate] window]
Run Code Online (Sandbox Code Playgroud)
但是,我发现它的缺点是它不会自动进行旋转.为了使其轮流,最好的方法是在当前的AppDelegate window.navigationController
/ 上添加视图rootViewController
,但是,通过这样做,它将不再是所有内容.假设当显示视图并且有一个modalViewController弹出窗口时,模态视图肯定会掩盖视图.
我的问题是,是否可以将子视图添加到最顶层的窗口并支持方向?在这种情况下我需要两组Nib文件吗?如何UIAlertView
结合最顶层和定位支持的魔力?当我查看时UIAlertView.h
,我看到实际上有2个UIWindows
,一个叫originalWindow
,另一个叫dimWindow
.有没有可以找到源代码的地方UIAlertView.m
?
Aus*_*tin 15
我发现的最佳解决方案是创建一个透明的容器视图,将该容器添加到窗口,并将警报放在容器中.然后,您可以注册UIApplicationWillChangeStatusBarOrientationNotification
接收轮换事件并转换容器; 这允许您独立操作警报的框架:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
self.container = [[UIView alloc] initWithFrame:self.window.bounds];
[self.container addSubview:self.alertView];
[self.window addSubview:self.container];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarWillRotate:)
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
...
}
...
- (void)statusBarWillRotate:(NSNotification *)theNotification
{
CGFloat rotation = 0;
switch ([notification[UIApplicationStatusBarOrientationUserInfoKey] intValue])
{
case UIInterfaceOrientationLandscapeLeft:
rotation = -M_PI_2;
break;
case UIInterfaceOrientationLandscapeRight:
rotation = M_PI_2;
break;
case UIInterfaceOrientationPortraitUpsideDown:
rotation = M_PI;
break;
case UIInterfaceOrientationPortrait:
default:
rotation = 0;
break;
}
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(rotation);
CGSize rotatedSize = CGRectApplyAffineTransform(self.window.bounds, rotationTransform).size;
[UIView animationWithDuration:[UIApplication sharedApplication].statusBarOrientationAnimationDuration
animations:^{
self.container.transform = rotationTransform;
// Transform invalidates the frame, so use bounds/center
self.container.bounds = CGRectMake(0, 0, rotatedSize.width, rotatedSize.height);
self.container.center = CGPointMake(self.window.bounds.size.width / 2, self.window.bounds.size.height / 2);
}];
}
Run Code Online (Sandbox Code Playgroud)
如果你真的想要,你可以创建一个全新的窗口,其windowLevel
属性设置为UIWindowLevelAlert
,这将保证窗口保持在所有其他视图之上,包括键盘,但窗口管理变得非常棘手.上述解决方案应该足以满足大多数需求.
简单地将视图控制器的视图添加到窗口的问题是,view[Will|Did][Disa|A]ppear:
除非通过根视图控制器将其添加到视图控制器层次结构中,否则无法保证接收所有旋转和消息addChildViewController:
.
只有第一个子视图UIWindow
被告知方向变化。(<iOS8)
你的问题与这个问题非常相似:
正如那里的答案所说,您可以注册方向更改的通知并以这种方式处理“顶部”子视图的重新布局。
归档时间: |
|
查看次数: |
39288 次 |
最近记录: |