如何使UITextView包含UIViewController的全宽和高度

时间:2015-02-27 03:31:07

标签: ios objective-c swift uiviewcontroller

我有UITextView我希望它的容器高度和宽度相同。这是一个简单的UIViewContainer 我尝试了以下操作:

override public func viewDidLoad() {
    self.edgesForExtendedLayout = .None
    resultText.text = result
    resultText.frame = view.frame
}

这似乎适用于肖像,但不适用于风景。 我所要做的就是让UITextView占用它容器的所有空间。

如果我能在Objective-C中找到答案,我可以很容易地将其翻译成Swift。我只是在寻找iOS的答案。

2 个答案:

答案 0 :(得分:3)

我建议你使用自动布局 enter image description here

然后点击添加4个约束。

如果有任何警告, enter image description here

点击更新框架

答案 1 :(得分:2)

Autolayout是您的朋友 - 可以使用Interface Builder或代码轻松完成:

override public func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = .None
    resultText.text = result

    // add vertical constraints to pin the view to the superview edge
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0.0-[resultText]-0.0-|", options: nil, metrics: nil, views: ["resultText": resultText]))

    // add horizontal constrains to pin the view to the superview edge
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0.0-[resultText]-0.0-|", options: nil, metrics: nil, views: ["resultText": resultText]))
}