UIKit参考说UIView是UIWindow的超类,但是尽管有这种父母,UIWindow实际上管理UIViews.这对我来说听起来很不寻常.
有谁知道这在软件设计方面的重要性是什么?
非常感谢.
编辑:
我阅读了iPhone编程指南中的相关段落.然而我无法理解他们为什么会这样做:让UIWindow成为UIView的父母.肯定有一些事情迫使Apple以这种方式设计类层次结构.
你如何从一个视图到另一个视图?是
[window addSubview:myView];
Run Code Online (Sandbox Code Playgroud)
唯一的选择还是有更好的方法来做到这一点?
我试图从UIViewController在AppDelegate的UIWindow中添加UILabel.这就是我这样做的方式:
AppDelegate代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}
[self.window makeKeyAndVisible];
self.window.rootViewController = self.viewController;
return YES;
}
Run Code Online (Sandbox Code Playgroud)
ViewController代码:
- (void)viewDidLoad
{
UILabel *abcd=[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 40.0)];
abcd.text=@"loading...";
abcd.backgroundColor=[UIColor clearColor];
[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];
[super viewDidLoad]; …Run Code Online (Sandbox Code Playgroud) 在iOS 5.1.1中,我发现如果我创建了我的UIWindow(我厌倦了IB),并将其框架设置为[UIScreen mainScreen] .bounds,则窗口会显示在状态栏下.但是如果我做同样的事情是iOS 6,它会出现在状态栏正下方的正确位置.
CGRect r = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame: r];
self.detailViewController = [[DetailViewController alloc] init];
self.window.rootViewController = self.detailViewController;
[self.window addSubview: detailViewController.view];
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
但是,如果我插入r.origin.y + = 20,要将窗口移动到iOS 5.1.1中的状态栏下方,这并不能解决问题,事情只会变得更奇怪.
在以编程方式创建UIWindow时,在UI中放置UIWindow的正确方法是什么?谢谢.
我正在尝试将一个CALayer添加为UIView子类中的子层,但是当我在init方法中添加子层EXC_BAD_ACCESS时,我将视图添加到另一个视图或窗口时得到.
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
if ((self = [super init]))
{
self.title = title;
self.message = message;
self.alertLayer = [[CALayer alloc] init];
self.layer.cornerRadius = kCORNER_RADIUS;
self.layer.shadowRadius = 3.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(15, 20);
self.layer.shadowOpacity = 1.0;
self.alertLayer.delegate = self;
self.alertLayer.masksToBounds = YES;
self.alertLayer.cornerRadius = kCORNER_RADIUS;
[self.layer addSublayer:self.alertLayer]; // This line of code seems to cause EXC_BAD_ACCESS
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
EXC_BAD_ACCESS[self.view addSubview:alertView]在视图控制器或UIWindow内部调用后引起.
我正在整合Pushwoosh SDK for Push Notification,它UIWindow用于表示从Pushwoosh门户发送的HTML页面
- (void) showWebView {
self.richPushWindow.alpha = 0.0f;
self.richPushWindow.windowLevel = UIWindowLevelStatusBar + 1.0f;
self.richPushWindow.hidden = NO;
self.richPushWindow.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.3 animations:^{
self.richPushWindow.transform = CGAffineTransformIdentity;
self.richPushWindow.alpha = 1.0f;
} completion:^(BOOL finished) {
}];
}
- (void) showPushPage:(NSString *)pageId {
NSString *url = [NSString stringWithFormat:kServiceHtmlContentFormatUrl, pageId];
HtmlWebViewController *vc = [[HtmlWebViewController alloc] initWithURLString:url];
vc.delegate = self;
vc.supportedOrientations = supportedOrientations;
self.richPushWindow.rootViewController = vc;
[vc view];
}
Run Code Online (Sandbox Code Playgroud)
在关闭HTML页面时,它会调用
self.richPushWindow.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.richPushWindow.transform …Run Code Online (Sandbox Code Playgroud) 我的主 ViewController viewDidLoad 函数中有以下代码
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
nav = [[NavWithAutoRotateViewController alloc] initWithRootViewController:self];
[window addSubview:[nav view]];
[window makeKeyAndVisible];
[[self navigationController] setNavigationBarHidden:YES];
Run Code Online (Sandbox Code Playgroud)
我的 ipad 应用程序当前设置为仅在横向模式下工作,我正在使用这个新窗口来显示快速查看文档并允许导航栏提供后退按钮和文档的保存选项。主要问题是新的 UIWindow 方向与我的主要应用程序 UIWindow 不匹配。
我上面有一个自定义的 UINavigationController,名为 NavWithAutoRotateController,这是该控制器的代码。
-(id)init
{
if(self)
{
// _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
// _orientation = UIInterfaceOrientationLandscapeLeft;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional …Run Code Online (Sandbox Code Playgroud) 首先,我的应用程序的设置:
大多数应用程序的视图控制器都存在于您的标准导航控制器层次结构中,但我还在主应用程序窗口上有第二个窗口,它主持一个视图控制器(NotificationVC).如果NotificationVC正在显示通知,它将更改状态栏样式以与通知形成对比,但否则它会将样式推迟到主窗口的根视图控制器.
我的问题是主窗口中的更改通常会触发状态栏外观更新(推送,弹出或呈现视图控制器或调用-[UIViewController setNeedsStatusBarAppearanceUpdate])无效.
以下是相关代码NotificationVC:
@implementation NotificationVC
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (self.isShowingNotification)
{
if (self.notificationView.hasDarkBackground)
{
return UIStatusBarStyleLightContent;
}
else
{
return UIStatusBarStyleDefault;
}
}
else
{
return [[UIApplication sharedApplication].delegate window].rootViewController.preferredStatusBarStyle;
}
}
@end
Run Code Online (Sandbox Code Playgroud)
如何让状态栏从主窗口中的一个视图控制器更新?
注意:手动设置状态栏外观(-[UIApplication setStatusBarStyle:])不适用于此应用程序.
我有一个iPad应用程序,我UIWindow在应用程序的开头创建一个新的,并在我进行一些资源同步时显示它.当iPad处于纵向方向时启动应用程序时,一切都很好.然而,当在横向方向上时,新创建的UIWindow's尺寸似乎很好,但它看起来是侧面的,它的坐标似乎都很奇怪.以下是纵向和横向方向的屏幕截图:


通过向右旋转一次获得景观.
我创建和显示的代码片段UIWindow如下:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame];
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
self.progressWindow.windowLevel = UIWindowLevelAlert;
/* some other code to create the subviews */
[self.progressWindow makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
此问题仅发生在iOS 8上.
我使用上下文UIWindow在我的应用程序中切换key ,以提供更简洁的流程-欢迎窗口=>具有项目列表的主屏幕<=>带有汉堡菜单和物品的项目容器。
函数示例如下:
- (void)updateKeyWindow:(UIWindow *)window withTransition:(WindowTransition)transition
{
UIWindow *originalWindow = _keyWindow;
_keyWindow = window;
window.alpha = 0;
[originalWindow resignKeyWindow];
[originalWindow resignFirstResponder];
originalWindow.userInteractionEnabled = NO;
[window makeKeyAndVisible];
window.transform = (transition == WindowTransitionFlyDown) ? CGAffineTransformMakeScale(1.02, 1.02) :
(transition == WindowTransitionFlyUp) ? CGAffineTransformMakeScale(.96, .96) :
CGAffineTransformIdentity;
// [UIView animateWithDuration:.24 animations:^{
window.alpha = 1;
originalWindow.alpha = 0;
window.transform = CGAffineTransformIdentity;
originalWindow.transform = (transition == WindowTransitionFlyDown) ? CGAffineTransformMakeScale(.96, .96) :
(transition == WindowTransitionFlyUp) ? CGAffineTransformMakeScale(1.02, 1.02) :
CGAffineTransformIdentity;
// } completion:^(BOOL b){ …Run Code Online (Sandbox Code Playgroud) uiwindow ×10
ios ×8
iphone ×3
uiview ×3
objective-c ×2
calayer ×1
cgrect ×1
ios5 ×1
ios6 ×1
ipad ×1
navigation ×1
quartz-2d ×1
uiscreen ×1
uistatusbar ×1