ios - Swift View 未显示

标签 ios swift

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

相关文章:

ios - 当 "Button Shapes"辅助功能打开时,UIBarButton 标题文本未显示在模态视图 Controller 上

ios - 为什么xcode中的图像在运行时占用的空间比实际大小多50倍

ios - 减少 swift/iOS 中的视频大小以上传到服务器

ios - 当导航栏半透明 = false 时 UISearchBar 超出屏幕边界

objective-c - 无法快速创建桥接头

ios - 更新 UILabel 不起作用

ios - 使用 Swift 在 UIWebView 内单击事件

ios - iOS-检测软件键盘高度

ios SWIFT 在解包选项值时发现 nil。

iOS 通知(推送器)