UIView功能错误

时间:2019-05-26 11:27:43

标签: ios swift

我正在尝试创建一个UIView函数,该函数将在调用时返回带有2个按钮的视图,即某些stackViews中的TextView。我遇到了麻烦,目前返回的只是一个空矩形(它内部没有显示任何信息)。我在做什么错了?

func returnView(text1: String, text2: String) -> UIView {

let customView = UIView()
let firstButton = UIButton()
let secondButton = UIButton()
let textView = UITextView()

customView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 90)
customView.backgroundColor = UIColor(red:0.82, green:0.83, blue:0.85, alpha:1.0)

firstButton.setTitle(text1, for: .normal)
firstButton.setTitleColor(UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0), for: .normal)
firstButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)

secondButton.setTitle("apple", for: .normal)
secondButton.setTitleColor(UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0), for: .normal)
secondButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)

textView.text = text2

let stack1 = UIStackView()
stack1.axis = .horizontal
stack1.alignment = .fill 
stack1.distribution = .fillEqually 

stack1.addArrangedSubview(firstButton)
stack1.addArrangedSubview(secondButton)

let stack2 = UIStackView()
stack2.axis = .horizontal
stack2.alignment = .fill 
stack2.distribution = .fillEqually

stack2.addArrangedSubview(textView)

let fullStack = UIStackView()
fullStack.axis = .vertical
fullStack.alignment = .fill
fullStack.distribution = .fillEqually
fullStack.addArrangedSubview(stack1)
fullStack.addArrangedSubview(stack2)

customView.addSubview(fullStack)

stack1.translatesAutoresizingMaskIntoConstraints = false
stack2.translatesAutoresizingMaskIntoConstraints = false
fullStack.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([fullStack.topAnchor.constraint(equalTo: customView.topAnchor), fullStack.leftAnchor.constraint(equalTo: customView.leftAnchor), fullStack.rightAnchor.constraint(equalTo: customView.rightAnchor), fullStack.heightAnchor.constraint(equalTo: customView.heightAnchor), (fullStack.centerXAnchor.constraint(equalTo: customView.centerXAnchor))])

return customView
}

编辑我是通过以下函数调用的:

func renderAttachment() {

    let view = returnView(text1: "test1", text2: "test2")

    // Render Image
    let renderer = UIGraphicsImageRenderer(size: view.frame.size)
    let image = renderer.image {
        view.layer.render(in: $0.cgContext)
    }

    // Create Attachment
    let attachment = NSTextAttachment()
    attachment.image = image
    attachment.bounds = CGRect(origin: .zero, size: image.size)

    // Current Attributed String
    let atStr = NSMutableAttributedString(attributedString: textview.attributedText)

    // Insert Attachment
    let attachmentAtStr = NSAttributedString(attachment: attachment)
    if let selectedRange = textview.selectedTextRange {
        let cursorIndex = textview.offset(from: textview.beginningOfDocument, to: selectedRange.start)
        atStr.insert(attachmentAtStr, at: cursorIndex)
        atStr.addAttributes(self.textview.typingAttributes, range: NSRange(location: cursorIndex, length: 1))
    } else {
        atStr.append(attachmentAtStr)
    }
    textview.attributedText = atStr
    print(atStr)
}

这就是它在我的textView中显示的方式(只是一个空框!)enter image description here

2 个答案:

答案 0 :(得分:1)

我试图使用您的函数添加一个视图,它工作得很好。请参见下面的屏幕截图。

enter image description here

在self.returnView(text1:“ Test1”,text2:“ Test2”)函数调用之后添加以下代码行。

view.layoutIfNeeded()

希望这会有所帮助。

答案 1 :(得分:1)

您正在呈现尚未添加到视图层次结构的视图,因此您需要通过调用view.layoutIfNeeded()来触发布局:

let view = returnView(text1: "test1", text2: "test2")
view.layoutIfNeeded()

// Render Image
let renderer = UIGraphicsImageRenderer(size: view.frame.size)
let image = renderer.image {
    view.layer.render(in: $0.cgContext)
}