ios - UIView 功能错误

标签 ios swift

我正在尝试创建一个 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/

相关文章:

ios - 从 iCloud 备份中排除 : NSURLIsExcludedFromBackupKey

ios - 将 View 作为 UITableViewCell 放在 tableView 中是一种好方法吗?

ios - Alamofire 不会在分段上传中生成网络丢失/超时错误

ios - 将一个球放入一个被困在里面的矩形内,并具有一定的速度和恢复力

ios - 在 Swift 中使用 xib 创建 UIView

ios - 如何获取 uiimage.image 属性更改与实际图像从 Web 出现之间的时间间隔

ios - 快速连续移动一个对象( Sprite 节点),直到在屏幕上被触摸

ios - 没有从 UITableView 获取所有 UITableViewCell

ios - 如何创建向用户显示一堆卡片的 Collection View ?

ios - NSCoder:无法识别的选择器发送到实例