窗口无法快速添加子视图

Posted

技术标签:

【中文标题】窗口无法快速添加子视图【英文标题】:window can't addsubview with swift 【发布时间】:2015-04-24 09:21:12 【问题描述】:

在函数 Appdelegate - didFinishLaunchingWithOptions 中实现:

var testView : UIView = UIView(frame: CGRectMake(100, 100, 100, 100))
testView.backgroundColor = UIColor.blueColor()
self.window?.addSubview(testView)
self.window?.bringSubviewToFront(testView)
self.window?.makeKeyAndVisible()

但不显示 testView。

【问题讨论】:

参考此视频youtube.com/watch?v=5ibVlOx2o7I 【参考方案1】:

您需要做的就是为您的窗口添加框架。

您的代码将是:

window = UIWindow(frame: UIScreen.mainScreen().bounds)

var testView : UIView = UIView(frame: CGRectMake(0 , 0, 100, 100))
testView.backgroundColor = UIColor.blueColor()
self.window?.addSubview(testView)
self.window?.bringSubviewToFront(testView)
self.window?.makeKeyAndVisible()

【讨论】:

TestView 显示正常,但屏幕为黑色,我需要将 testView 带入导航控制器的字体中。 (testView 是页脚底部)。 tks! 你的问题是如何显示测试视图。您没有提及其他任何内容。如果可行,请接受答案..:)

以上是关于窗口无法快速添加子视图的主要内容,如果未能解决你的问题,请参考以下文章

Appcelerator Titanium 3.x/IOS7 无法将窗口添加为视图的子项

无法连接到 IB 中的子视图

快速创建多个子视图时添加动画

iOS:从 UIGestureRecognizer 崩溃的关键窗口子视图元素

无法将 3D 子视图添加到控制器的视图中

如何将子视图从其父视图的父视图置于顶部