gpt4 book ai didi

ios - Swift View 未显示

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:01 26 4
gpt4 key购买 nike

我试图在我的主视图上创建一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com