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是主 View 的 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/

相关文章:

iphone - 用于 iOS 开发的 Objective-C++

ios - Firebase 崩溃报告与 Crashlytics/Fabric

ios - AirPods 无法用作录音机应用程序的输入源

返回选择器字典的 Swift 协议(protocol)

ios - 自动布局 : Set height View depends on other height View

ios - 如何将 subview 添加到 uiscrollview 以开始内容(在第一个 subview 之前)?

ios - 如果我执行 unwind segue (show),母亲 View Controller 如何获得 child 的 View Controller ?

ios - 使用特定采样率捕获音频样本,如 iOS Swift 中的 Android

swift - Collection View 中的图像

swift - case语句中 Unresolved 标识符错误