【问题标题】:How to make a UITextView full width and height of containing UIViewController如何使包含 UIViewController 的 UITextView 全宽和全高
【发布时间】:2015-02-27 03:31:06
【问题描述】:

我有UITextView,我想让它的容器具有相同的高度和宽度。它是一个简单的UIViewContainer
我尝试执行以下操作:

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

这似乎适用于纵向但不适用于横向。 我要做的就是让 UITextView 占用它容器的所有空间。

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

【问题讨论】:

  • UITextView *textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)] 不起作用?
  • @Ra1nWarden 如何考虑顶部导航工具栏的高度?
  • @Johnston:基于@Ra1nWarden 解决方案:UITextView *textView = [[UITextView alloc] initWithFrame: CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.frame.size.width, self.frame.size.height)] 这应该照顾导航栏。

标签: ios objective-c swift uiviewcontroller


【解决方案1】:

我建议你使用自动布局

然后点击添加 4 个约束。

如果有任何警告,

点击更新帧

【讨论】:

    【解决方案2】:

    自动布局是您的朋友 - 可以使用 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]))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多