我试图在我的主视图上创建一个 UITextView
,我设法用 textView = UITextView(frame: CGRect(x: 10, y: 10, width: 300, height: 300))
但是,由于某些原因,我不能用约束来做到这一点。这是我在 viewDidLoad()
中调用的函数:
private func configureTextView() {
view.addSubview(textView)
textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10)
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10)
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10)
textView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10)
textView.backgroundColor = .red
print(view.subviews)
}
这是 textView
:
private let textView: UITextView = {
let view = UITextView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
我还是个初学者,但是 print(view.subviews)
显示 textView
是主视图的 subview (我猜)
[<UITextView: 0x7fdf64809600; frame = (0 0; 0 0); text = '';
clipsToBounds = YES; gestureRecognizers = <NSArray: 0x60000062f450>;
layer = <CALayer: 0x60000083b360>; contentOffset: {0, 0}; contentSize:
{0, -8}; adjustedContentInset: {0, 0, 0, 0}>]
最佳答案
您创建了约束,但从未将它们设置为事件的。更改为此应该有助于使其正确显示:
textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
textView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10).isActive = true
关于ios - Swift View 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575454/