从 UIStackView 中缺少的代码添加 UITextView
UITextView added from code missing in UIStackView
我正在尝试以编程方式将 UITextView
添加到 UIStackView
,但它没有出现在屏幕上。
当我签入 Debug View Hierarchy 时,它位于视图层次结构中并且有一些约束但未显示在输出视图中。
这是我的代码:
let stackView = UIStackView()
let textView = UITextView()
let button = UIButton()
stackView.axis = .Horizontal
stackView.spacing = 20
stackView.distribution = UIStackViewDistribution.Fill
button.setImage(UIImage(named:"image1"), forState: .Normal)
button.setImage(UIImage(named:"image2"), forState: .Selected)
textView.textAlignment = NSTextAlignment.Left
textView.textColor = UIColor.blackColor()
textView.editable = true
textView.text = ""
stackView.addArrangedSubview(textView)
stackView.addArrangedSubview(button)
按钮添加得很好。但是我找不到正确显示 TextView 的方法!尝试添加 width/height 约束,如下所示,但效果不佳或效果不佳(取决于变体):
let widthConstraint = NSLayoutConstraint(item: textView,
attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: stackView,
attribute: NSLayoutAttribute.Width,
multiplier: 1,
constant: 0)
stackView.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: stackView, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
stackView.addConstraint(heightConstraint)
但是,当我添加 UITextField
而不是 UITextView
时,它可以正常工作,没有任何限制。
尝试使用
init(frame frame: CGRect,textContainer textContainer: NSTextContainer?)
因为它是 UITextView 的指定初始化程序。
我想出了缺少的东西。我无法使用指定的初始值设定项,因为我不知道 class 我正在创建它的文本视图的框架大小。
但这有帮助 - 在上面的代码中 stackView.distribution 之后添加这一行:
self.alignment = UIStackViewAlignment.Fill
UITextView
是 UIScrollView
的子类,因此它的大小在 UIStackVIew
中不明确。为了使大小明确,您可以根据其内容自行调整大小 - 即不可滚动。
简单的解决方案是:
textView.isScrollEnabled = false
(Swift 3.0 语法)
我正在尝试以编程方式将 UITextView
添加到 UIStackView
,但它没有出现在屏幕上。
当我签入 Debug View Hierarchy 时,它位于视图层次结构中并且有一些约束但未显示在输出视图中。
这是我的代码:
let stackView = UIStackView()
let textView = UITextView()
let button = UIButton()
stackView.axis = .Horizontal
stackView.spacing = 20
stackView.distribution = UIStackViewDistribution.Fill
button.setImage(UIImage(named:"image1"), forState: .Normal)
button.setImage(UIImage(named:"image2"), forState: .Selected)
textView.textAlignment = NSTextAlignment.Left
textView.textColor = UIColor.blackColor()
textView.editable = true
textView.text = ""
stackView.addArrangedSubview(textView)
stackView.addArrangedSubview(button)
按钮添加得很好。但是我找不到正确显示 TextView 的方法!尝试添加 width/height 约束,如下所示,但效果不佳或效果不佳(取决于变体):
let widthConstraint = NSLayoutConstraint(item: textView,
attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: stackView,
attribute: NSLayoutAttribute.Width,
multiplier: 1,
constant: 0)
stackView.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: stackView, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
stackView.addConstraint(heightConstraint)
但是,当我添加 UITextField
而不是 UITextView
时,它可以正常工作,没有任何限制。
尝试使用
init(frame frame: CGRect,textContainer textContainer: NSTextContainer?)
因为它是 UITextView 的指定初始化程序。
我想出了缺少的东西。我无法使用指定的初始值设定项,因为我不知道 class 我正在创建它的文本视图的框架大小。
但这有帮助 - 在上面的代码中 stackView.distribution 之后添加这一行:
self.alignment = UIStackViewAlignment.Fill
UITextView
是 UIScrollView
的子类,因此它的大小在 UIStackVIew
中不明确。为了使大小明确,您可以根据其内容自行调整大小 - 即不可滚动。
简单的解决方案是:
textView.isScrollEnabled = false
(Swift 3.0 语法)