gpt4 book ai didi

ios - UIView 功能错误

转载 作者:行者123 更新时间:2023-11-28 11:33:20 25 4
gpt4 key购买 nike

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

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

最佳答案

我尝试使用您的函数添加一个 View ,但效果非常好。请看下面的截图。

enter image description here

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

view.layoutIfNeeded()

希望这对您有所帮助。

关于ios - UIView 功能错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313081/

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