我试图在iPhone的UIStatusBar上检测到任何触摸,但它无法正常工作.我已经尝试了子类化UIApplication和UIWindow来访问它但仍然没有
在创建基于Xcode 4视图的新项目时,它会在applicationDelegate didFinishLaunching方法上创建该行:
self.window.rootViewController = self.viewController;
Run Code Online (Sandbox Code Playgroud)
但该属性rootViewController仅在iOS 4.0中出现.
我可以在这里写些什么来与4.0之前的iOS兼容?
PS:由于一些问题,我将无法测试解决方案,所以请它应该没问题.
iphone backwards-compatibility uiviewcontroller uiwindow ios
我想知道在所有iOS应用程序中我们必须为UIWindow对象编写makeKeyAndVisible.那么有一个例子,我们不希望我们的应用程序makeKeyAndVisible.
如果我们总是要求它用于任何应用程序,那么为什么dint apple会自动为我们编写它,就像它们为iOS 6中的@synthesize所做的那样?
要获取Objective-c中的rootViewController,您可以使用下面的代码行
[[[UIApplication sharedApplication] delegate] window].rootViewController
Run Code Online (Sandbox Code Playgroud)
我试图在swift中做同样的事情
UIApplication.sharedApplication().delegate?.window?.rootViewController
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误
"的UIWindow?没有名为'rootViewController'的成员
和建议说,你必须解开一个UIWindow两次UIWindow??是
UIApplication.sharedApplication().delegate?.window??.rootViewController
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么我需要打开窗口两次?
.
.
我查看了API并发现了
protocol UIApplicationDelegate : NSObjectProtocol {
optional var window: UIWindow? { get set }
}
Run Code Online (Sandbox Code Playgroud)
窗口有一个可选的
class UIWindow : UIView {
var rootViewController: UIViewController?
}
Run Code Online (Sandbox Code Playgroud)
rootViewController也有一个可选项
.
.
我想可能是因为UIApplicationDelegate协议中的UIWindow已经有了optional,UIWindow?所以我在Playground中尝试了以下内容
@objc protocol MyApplicationDelegate {
optional var myWindow: MyWindow? { get set }
}
class MyWindow : NSObject {
var rootViewController: Int?
init(number: Int) {
rootViewController = number
}
}
class …Run Code Online (Sandbox Code Playgroud) 我的应用程序驱动第二个屏幕(外部显示器),但我看到一些关于旋转的"奇怪"事情(在iOS7上没有发生的事情)
如果我以横向方向启动应用程序(并连接第二个屏幕),然后按主页按钮将应用程序放入后台,然后重新打开应用程序,然后将第二个屏幕(连接到显示器)旋转90度并仅使用一半的屏幕.没有任何后续旋转修复此问题.
我非常有信心这是一个错误 - 但我很乐意另外知道.下面是在简单的单视图应用程序中重现它的代码.
谢谢
@interface AppDelegate ()
@property (nonatomic, strong) UIWindow* externalWindow;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidConnect:) name:UIScreenDidConnectNotification object:nil];
UIScreen* externalScreen = ([UIScreen screens].count > 1 ? [[UIScreen screens] objectAtIndex:1] : nil);
if (externalScreen)
{
[self setupExternalScreen:externalScreen];
}
return YES;
}
- (void) screenDidConnect:(NSNotification *)aNotification
{
UIScreen* externalScreen = (UIScreen*)aNotification.object;
[self setupExternalScreen:externalScreen];
}
- (void)setupExternalScreen:(UIScreen*)externalScreen
{
externalScreen.currentMode = externalScreen.preferredMode;
self.externalWindow = [[UIWindow alloc] initWithFrame:externalScreen.bounds];
self.externalWindow.screen = externalScreen;
self.externalWindow.clipsToBounds = YES; …Run Code Online (Sandbox Code Playgroud) 我见过许多 UIWindow 被声明为可选变量的例子,就像这样,
var window: UIWindow?
我的应用程序只有一个窗口,并且在整个生命周期中它将保持不变。我认为将其声明为常量更有意义。我也这么做了。它不会引发任何编译器错误(从 iOS 8.2 开始)并且似乎工作得很好。
为什么没有其他人这样做?这样做有什么陷阱吗?
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let window: UIWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = ViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()
return true
}
Run Code Online (Sandbox Code Playgroud) 我一直在使用以下帖子作为如何显示UIAlertController与特定无关的from代码的指导UIViewController。现在,我要对该代码进行单元测试:
func showAlert(alert: UIAlertController, animated: Bool, completion: (()->Void)?)
{
let alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
// Keep a strong refence to the window.
// We manually set this to nil when the alert is dismissed
self.alertWindow = alertWindow
alertWindow.rootViewController = UIViewController()
if let currentTopWindow = UIApplication.sharedApplication().windows.last {
alertWindow.windowLevel = currentTopWindow.windowLevel + 1
}
else {
// This case only happens during unit testing
Logger.trace(ICELogLevel.Error, category: .Utility, message: "The application doesn't have a window being displayed!") …Run Code Online (Sandbox Code Playgroud) 在appdidfinishlaunch我正在加载一个tabbarcontroller作为子视图,然后我加载另一个视图
MySubView * mySubView = [[MySubView alloc] init];
[window addSubview:mySubView];
[mySubView release];
Run Code Online (Sandbox Code Playgroud)
我想通过子视图中的buttonclick关闭该toplayer,所以我设置了一个IBAction并尝试了不同的东西来强制实际视图关闭:
// 1.
[self.view removeFromSuperview];
// 2.
id *delegate = [[UIApplication sharedApplication] delegate];
[[[delegate view] objectAtIndex:0] removeFromSuperview];
//3.
[[[delegate window] view] removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)
那么如何从窗口中弹出这个子视图呢?
西蒙欢呼
我想删除1个子视图并不是全部.那个子视图是UIWebView.
我正在显示一个新的UIWindow,并且希望它从左侧滑入并在关闭时滑入左侧。如何设置UIWindow的显示和移除动画效果?
这是我当前显示新UIWindow的方式。
- (void)showMenu
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UIButton *closeMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeMenuButton setFrame:CGRectMake(250, 10, 50, 50)];
[closeMenuButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
[closeMenuButton addTarget:self action:@selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];
blurredView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[blurredView setBarStyle:UIBarStyleBlack];
MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:@"MenuTableViewController" bundle:nil];
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);
menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:blurredView];
[blurredView addSubview:closeMenuButton];
[blurredView …Run Code Online (Sandbox Code Playgroud) uiwindow ×10
ios ×8
iphone ×4
objective-c ×3
swift ×3
animation ×1
appdelegate ×1
cocoa-touch ×1
uiscreen ×1
uiview ×1
uiwebview ×1
unit-testing ×1
xcode ×1