我正在尝试将我managedObjectContext的传递给下一个控制器。我UIWindow在 appDelegate 文件中生成了一个实例,因为我需要获取我的备用控制器。但是,Xcode 说我的UIWindow实例为零。
这个 id 我的代码:
lazy var managedObjectContext: NSManagedObjectContext = persistentContainer.viewContext
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabController = window!.rootViewController as! UITabBarController
if let tabViewControllers = tabController.viewControllers {
let navController = tabViewControllers[0] as! UINavigationController
let controller = navController.viewControllers.first as! CurrentLocationViewController
controller.managedObjectContext = managedObjectContext
}
return true
}
Run Code Online (Sandbox Code Playgroud)
这有点奇怪。如何解决这个问题?提前致谢。
我UIWindow最初有白色背景.我想在设备旋转时将背景更改为蓝色(永久).但实际发生的是颜色短暂闪烁蓝色然后又回到白色.
在app委托中:
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
{
self.window.backgroundColor = [UIColor blueColor];
}
Run Code Online (Sandbox Code Playgroud)
这个代码按预期调用,但是当旋转完成时,-[UIWindow setBackgroundColor:]第二次调用(正如我通过子类化发现的那样UIWindow).第二个调用的调用堆栈是:
-[UIWindow setBackgroundColor]
-[UIWindow _finishedFullRotation:finished:context:]
-[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
-[UIViewAnimationState animationDidStop:finished:]
-run_animation_callbacks
...
Run Code Online (Sandbox Code Playgroud)
编辑(@ VinceBurn的答案)
该应用程序只有一个视图(来自Xcode的基于视图的应用程序项目模板).我现在已将视图的背景颜色(在IB中)设置为0%不透明度.仍然得到相同的结果.
为了确保白色不是来自其他一些默认颜色设置,我现在最初将窗口的背景颜色设置为绿色:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
self.window.backgroundColor = [UIColor greenColor];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
旋转时,它会短暂闪烁蓝色但返回绿色.
我正在写一个iPad应用程序,我正试图UIWindow在我的应用程序的主窗口顶部显示第二个.我要做的主要是创建一个登录窗口(如何使用UISplitViewController呈现登录?),似乎在这里创建第二个窗口可能是一个不错的选择.
我做了一个非常简单的应用程序试试这个.当用户点击按钮时,我正试图显示第二个窗口.这是代码:
- (IBAction)showOtherWindow:(id)sender {
UIWindow* otherWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
otherWindow.hidden = NO;
otherWindow.clipsToBounds = YES;
otherWindow.windowLevel = UIWindowLevelStatusBar;
otherWindow.backgroundColor = [UIColor redColor];
[otherWindow makeKeyAndVisible];
}
Run Code Online (Sandbox Code Playgroud)
我期待在这里看到一个大红色屏幕,但这不会发生 - 没有任何变化.最后,我想让一个较小的窗户浮在上面.但是现在我只想看到一个窗口.
我想一个添加UIView 到UIWindow使用appDelegate我使用这个代码:
NSArray *myViewArray = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
UIView *myView = myViewArray[0];
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
myView.frame = CGRectMake(0, 0, 258, 564);
[myView setTag:100];
[window addSubview:myView];
Run Code Online (Sandbox Code Playgroud)
但这不起作用.如果我将它添加到我的self.view工作正常.但是,我希望此视图在出现时显示在所有其他视图上.
我在这做错了什么?
编辑
我使用了上一个SO问题中的代码:如何在UIBarButton项目下添加UIImage
它工作正常,没有问题.
但是在这个只有viewDidLoad方法和一个控制器的新项目中它不起作用.
我正在寻找适当的方式来拍摄整个iPhone屏幕,包括键盘.
我找到了一些快照屏幕的代码:
CGRect screenCaptureRect = [UIScreen mainScreen].bounds;
UIView *viewWhereYouWantToScreenCapture = [[UIApplication sharedApplication] keyWindow];
//screen capture code
UIGraphicsBeginImageContextWithOptions(screenCaptureRect.size, NO, [UIScreen mainScreen].scale);
[viewWhereYouWantToScreenCapture drawViewHierarchyInRect:screenCaptureRect afterScreenUpdates:NO];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
但它也没有抓住键盘.它看起来像keyWindow不包括键盘视图.
顺便说一下,我需要UIImage最终结果,UIView所以我不能使用其他快照API.
有任何想法,并以最佳表现?
有没有办法消除当前提供的所有UIAlertControllers?
这特别是因为在我的应用程序的任何位置和任何状态下,当按下推送通知时,我都需要进入某个ViewController。
我有一个html-javascript页面,我需要检测它何时在Web视图上打开(例如在facebook webview,twitter webview等内部),以及是否是webview-显示其他内容。
注意:我无法控制第三方Android应用程序,因此无法更改其代码。
我已经找到了一种检测IOS Web视图的方法(在stackoverflow上找到它):
var isIosWebview =/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent)
现在,我正在寻找可以检测Android Web视图的JavaScript代码。
救命?
我的应用程序中需要三个不同的部分:登录屏幕,主屏幕和横向模式下的部分,完全不同.我看到Apple坚持认为应用程序应该有一个窗口,所以我问:最适合用于什么?三个大窗户,还是景色?层次结构应该如何?我没有图层的经验.虽然登录面板可以在用户登录后消失,但其他两个必须保持不变,无论哪一个都可见.
此外,无论您的答案是什么,新的UIWindow流程究竟会如何?如何附加它而不是最初的?他们有层?等等
谢谢!
我想在当前项目中实现会话时间功能.所以为此我尝试子类化UIWindow并覆盖touchesBegan和touchesEnded方法.
class BaseWindow: UIWindow {
convenience init() {
self.init(frame: UIScreen.mainScreen().bounds)
}
private var sessionTimeOutTimer: NSTimer?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
sessionTimeOutTimer?.invalidate()
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
sessionTimeOutTimer = NSTimer.scheduledTimerWithTimeInterval(60, target: CIAppManager.sharedManger(), selector: #selector(CIAppManager.sessionTimeOut), userInfo: nil, repeats: false)
}
}
Run Code Online (Sandbox Code Playgroud)
在app委托中,我做到了这一点
private let baseWindow = BaseWindow()
var window: UIWindow? {
get {
return baseWindow
}
set {}
}
Run Code Online (Sandbox Code Playgroud)
但问题是touchesBegan,touchesEnded并没有得到调用.我无法弄清楚我做错了什么.
uiwindow ×10
ios ×8
swift ×3
ipad ×2
iphone ×2
objective-c ×2
android ×1
appdelegate ×1
cocoa-touch ×1
html ×1
ios13 ×1
javascript ×1
snapshot ×1
touchesbegan ×1
touchesended ×1
uikeyboard ×1
uikit ×1
uiscreen ×1
uiview ×1
webview ×1