【发布时间】:2017-04-01 11:31:28
【问题描述】:
我的 Playground Book 中有一个 UIViewController,我想在上面放置一个 UILabel 和 UIView。
在 Playground Book 中,用户可以通过手势调整UIViewController 的大小,但内容(UILabel 和UIView)无法调整大小。这意味着UILabel 和UIView 有时比实际的UIViewController 大。 UILabel 和 UIView 有时也需要关闭。
我像这样创建了我的UIViewController
let window1 = WMWindow(frame: CGRectMake(150,150,400,300))
window1.title = "View 2"
let vc1 = UIViewController()
vc1.title = "View 2"
vc1.view = UIView()
vc1.view.backgroundColor = UIColor.white
let nc1 = UINavigationController(rootViewController: vc1)
window1.rootViewController = nc1
window1.makeKeyAndVisible()
window.addSubview(window1)
和我的UILabel & UIView 这样
let textLabel = UILabel(frame: CGRect(x: 30, y: window1.bounds.height-50, width: window1.bounds.width, height: window1.bounds.height))
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 5
textLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
let backgroundView = UIView(frame: CGRect(x: 0, y: window1.bounds.height, width: window1.bounds.width, height: 200))
backgroundView.backgroundColor = #colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
backgroundView.alpha = 0.7
当然,我将它们作为子视图添加到 UIViewController。
现在如何更改 UILabel 和 UIView 以根据 UIViewController 的大小自动调整大小?
我发现我可以使用vc1.view.addSubview(...) 代替window1.addSubview(...)。此行使用户能够正确调整视图和子视图的大小,但不幸的是,几秒钟后,子视图将位于与开头相同的位置。
【问题讨论】:
标签: ios swift cocoa-touch uiviewcontroller