在添加到UIWindow的视图上使用autolayout
我正在尝试将子视图添加到应用程序的keyWindow
,并使用autolayout定位它。 但是,autolayout似乎根本不起作用,而设置框架则可以。 我想使用以下代码将我的视图infoSc
与infoSc
的底部对齐:
let infoSc = InfoScreenView() infoSc.translatesAutoresizingMaskIntoConstraints = false let keyWindow = UIApplication.sharedApplication().keyWindow! keyWindow.addSubview(infoSc) keyWindow.addConstraint(NSLayoutConstraint(item: infoSc, attribute: .Left, relatedBy: .Equal, toItem: keyWindow, attribute: .Left, multiplier: 1, constant: 0)) keyWindow.addConstraint(NSLayoutConstraint(item: infoSc, attribute: .Right, relatedBy: .Equal, toItem: keyWindow, attribute: .Right, multiplier: 1, constant: 0)) keyWindow.addConstraint(NSLayoutConstraint(item: infoSc, attribute: .Bottom, relatedBy: .Equal, toItem: keyWindow, attribute: .Bottom, multiplier: 1, constant: 0)) infoSc.addConstraint(NSLayoutConstraint(item: infoSc, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
但是,它似乎有一个使用此方法的CGRectZero
框架。 任何想法如何使这项工作? 理想情况下,我也想将它与self.view
对齐,但是会抛出一个错误,即keyWindow
的视图层次结构中。
如果你需要在整个窗口上绘图,这里有代码(在这个例子中我在AppDelegate
,因此窗口是AppDelegate.window
属性)。
func tryToDrawOnTheWindow() { if let window = window, view = window.rootViewController?.view { print("I have a root view") let infoSc = InfoScreenView(frame: view.frame) let count = view.subviews.count view.insertSubview(infoSc, atIndex: count) infoSc.translatesAutoresizingMaskIntoConstraints = false let height = NSLayoutConstraint(item: infoSc, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: 0) let width = NSLayoutConstraint(item: infoSc, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0) let offset = NSLayoutConstraint(item: infoSc, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0) print([height, width, offset]) view.addConstraints([height, width, offset]) } else { print("No root view on which to draw") } }
这将允许您在视图层次结构中的任何内容上绘制。 在我的测试应用程序中,我添加了一个文本字段和一个蓝色矩形,覆盖图为橙色,不透明度为40%。 请记住,默认情况下,覆盖视图将消耗此情况下的所有分接头。
- 警告:尝试在*上呈现View Controller,它已经呈现
- 检测iOS应用程序进入后台
- IOS – 来自本地HTML文件的WKWebView中的CORS
- 使用Swift在Sprite Kit中构build棋盘游戏?
- estimatedHeightForRowAtIndexPath实际上可以改变一行的最终“正确”高度吗?
- Swift:即使video看起来很锐利,从video中提取图像也很模糊。
- viewDidUnload不调用alloc / init初始化的viewcontroller没有xib文件
- 为什么NSInteger而不是NSUInteger在“numberOfSectionInTableView:”?
- 模态视图控制器 – 如何显示和解除