Ios UIView函数错误
我试图创建一个UIView函数,调用该函数时将返回一个带有2个按钮的视图,在某些StackView中是一个TextView。我遇到了麻烦,目前它返回的只是一个空矩形(里面没有显示任何信息)。我做错了什么Ios UIView函数错误,ios,swift,Ios,Swift,我试图创建一个UIView函数,调用该函数时将返回一个带有2个按钮的视图,在某些StackView中是一个TextView。我遇到了麻烦,目前它返回的只是一个空矩形(里面没有显示任何信息)。我做错了什么 func returnView(text1: String, text2: String) -> UIView { let customView = UIView() let firstButton = UIButton() let secondButton = UIButton() l
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)
}
这就是它在我的文本视图(只是一个空框!)中的显示方式。我尝试使用您的函数添加一个视图,效果非常好。请看下面的截图 在self.returnView(text1:“Test1”,text2:“Test2”)函数调用后添加以下代码行
view.layoutIfNeeded()
希望这能有所帮助。我尝试使用您的函数添加一个视图,效果非常好。请看下面的截图 在self.returnView(text1:“Test1”,text2:“Test2”)函数调用后添加以下代码行
view.layoutIfNeeded()
希望这有帮助。您正在渲染一个尚未添加到视图层次结构的视图,因此需要通过调用
视图触发布局。layoutIfNeeded()
:
您正在呈现尚未添加到视图层次结构中的视图,因此需要通过调用
view.layoutifneed()
,触发布局:
我将您的函数添加到一个新项目中,并将
view.addSubview(returnView(text1:“Hello World!”,text2:“Foo Bar Baz”)
添加到viewDidLoad()
中,您的视图确实显示了内容。请注意,您的框架将视图放置在屏幕左上角的iPhone X的标题栏和凹口后面。我将您的函数添加到一个新项目中,并添加了view.addSubview(returnView(text1:“Hello World!”,text2:“Foo bar Baz”)
到viewDidLoad()
,您的视图确实显示了内容。请注意,您的框架将视图放置在屏幕左上角的标题栏和iPhone X的凹口后面。添加了包含更多信息的编辑,告诉我您的想法?添加了包含更多信息的编辑,告诉我您的想法?