gpt4 book ai didi

ios - 如何在具有不同内容的 Controller 中多次使用自定义 UIView?

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

我为必须在屏幕上显示的弹出窗口创建了自己的自定义 UIView。我的 UIView 看起来像这样:

class HelpTipsPopover: UIView {

weak var title: UILabel!
weak var myText: UILabel!

override init(frame: CGRect) {
super.init(frame: frame)

let strongTitle = UILabel()
title = strongTitle
let strongMyText = UILabel()
myText = strongMyText

self.addSubview(strongTitle)
title.translatesAutoresizingMaskIntoConstraints = false
if selected == true{
title.text = "Search"
title.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
title.textColor = UIColor.TRLMBlueBlackColor()


let leftConstraint = NSLayoutConstraint(item: title, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 10)
let topConstraint = NSLayoutConstraint(item: title, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 10)
self.addConstraints([leftConstraint, topConstraint])

self.addSubview(strongMyText)
myText.translatesAutoresizingMaskIntoConstraints = false
myText.text = "Search equities to view the order book and market prints for specific moments in time."
myText.numberOfLines = 0
myText.lineBreakMode = NSLineBreakMode.ByWordWrapping
myText.font = UIFont(name: "Helvetica Neue", size: 12)
myText.textColor = UIColor.TRLMBlueBlackColor()



let leftDescription = NSLayoutConstraint(item: myText, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: 10)
let rightDescription = NSLayoutConstraint(item: myText, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1.0, constant: 10)
let topDescription = NSLayoutConstraint(item: myText, attribute: .Top, relatedBy: .Equal, toItem: title, attribute: .Bottom, multiplier: 1.0, constant: 5)
self.addConstraints([leftDescription, topDescription, rightDescription])

}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}

}

现在我必须在我的 View Controller 中显示三个弹出窗口,每个弹出窗口中的标题和文本都不同。以下是我的 View Controller 中显示这些弹出窗口的方法:

 func showPopover(){
self.view.addSubview(helpTipsPopover)
helpTipsPopover.tag = 1
helpTipsPopover.translatesAutoresizingMaskIntoConstraints = false
helpTipsPopover.layer.cornerRadius = 6
helpTipsPopover.backgroundColor = UIColor(white: 1.0, alpha: 0.8)
let leftConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 10)
let topConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Top, relatedBy: .Equal, toItem: self.hotSpotOne, attribute: .Bottom, multiplier: 1.0, constant: 4)
let widthConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
let heightConstraint = NSLayoutConstraint(item: self.helpTipsPopover, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80)
self.view.addConstraints([leftConstraint, topConstraint, widthConstraint, heightConstraint])
}


func showPopoverTwo(){
self.view.addSubview(helpTipsPopover)
helpTipsPopover.tag = 1
helpTipsPopover.translatesAutoresizingMaskIntoConstraints = false
helpTipsPopover.layer.cornerRadius = 6
helpTipsPopover.backgroundColor = UIColor(white: 1.0, alpha: 0.8)
let centerConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0)
let topConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: .Top, relatedBy: .Equal, toItem: self.hotSpotTwo, attribute: .Bottom, multiplier: 1.0, constant: 4)
let widthConstraint = NSLayoutConstraint(item: helpTipsPopover, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
let heightConstraint = NSLayoutConstraint(item: self.helpTipsPopover, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 80)
self.view.addConstraints([centerConstraint, topConstraint, widthConstraint, heightConstraint])

}

现在我想实现这样的目标:

enter image description here

所以每个弹出窗口应该有不同的标题和文本,但我想重用相同的 UIView。任何帮助表示赞赏。谢谢!

最佳答案

修改你的初始化方法

init(frame: CGRect, titleString: String, bodyString: String) {
super.init(frame: frame)
// Your current initializer

title.text = titleString
myText.text = bodyString

}

然后你可以像这样初始化一个弹出窗口:

let frame = CGRectMake(0,0,180,100)
let titleString = "My Custom Title"
let bodyString = "This is a body. I'm explaining things to you here."
helpTipsPopoverOne = HelpTipsPopover(frame: frame, titleString: titleString, bodyString: bodyString)

修改您的显示方法以将弹出窗口作为参数。然后您可以创建弹出窗口并显示您想要的任何文本!

关于ios - 如何在具有不同内容的 Controller 中多次使用自定义 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32853122/

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