gpt4 book ai didi

ios - 如何创建一个自定义的 UIView,它只有一个按钮,然后可以在整个应用程序中将其放置在不同的位置使用?

转载 作者:行者123 更新时间:2023-11-28 08:56:40 40 4
gpt4 key购买 nike

我已经创建了一个圆形按钮,它是一个自定义 UIView。这是代码:

 class HelpTips: UIView {

weak var hotSpot: UIButton!

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

let strongHotSpot = UIButton()
hotSpot = strongHotSpot

self.addSubview(strongHotSpot)
hotSpotOne.translatesAutoresizingMaskIntoConstraints = false
hotSpotOne.backgroundColor = UIColor.TRLMHelpTipYellowColor()
hotSpotOne.layer.borderColor = UIColor.TRLMHelpTipStrokeColor().CGColor
hotSpotOne.layer.borderWidth = 1

let horizontalConstraint = NSLayoutConstraint(item: hotSpot, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1.0, constant: -1)
let verticalConstraint = NSLayoutConstraint(item: hotSpot, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 16)
let widthConstraint = NSLayoutConstraint(item: hotSpot, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 40)
let heightConstraint = NSLayoutConstraint(item: hotSpot, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 40)

self.addConstraints([verticalConstraint, horizontalConstraint, widthConstraint, heightConstraint])

}

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

现在这个相同的按钮在整个应用程序的多个位置使用,但它被放置在不同的位置。所以每个 View Controller 都会使用那个 UIView。所以从技术上讲,按钮的外观保持不变,但该按钮的约束会根据其位置不断变化。我想在这里遵循 DRY(不要重复自己)技术。我以前做过这种事情,但是代码重复了几次,效率不高。怎么办?任何帮助表示赞赏。谢谢!

最佳答案

您可以创建自定义导航 Controller 并将 View 作为属性存储在其中。每个 View Controller 都可以访问导航 Controller ,因此它们可以只引用该属性和您需要使用 View 的时间。应该保持干燥。

关于ios - 如何创建一个自定义的 UIView,它只有一个按钮,然后可以在整个应用程序中将其放置在不同的位置使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32892409/

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