gpt4 book ai didi

ios - 如何将分段 Controller 按钮从 UILabel 更改为 UIImage?

转载 作者:行者123 更新时间:2023-11-30 13:45:01 29 4
gpt4 key购买 nike

我按照 youtube 上的本教程创建了一个自定义分段 Controller :https://www.youtube.com/watch?v=qT1ZEE2CBDQ

我已成功创建它并使其正常运行。但现在我想将 UILabel 更改为图像图标。

我尝试了几件事,例如,将“var labels : [String] = ["a", "b", "c"] "更改为 "var labels = UIImage(named: "..") 等”

这可能不正确,但值得一试!

这是我当前的代码,其中显示 UILabels:

@IBDesignable class ADVSegmentedControl: UIControl {

private var labels = [UILabel]()



// var items: [UIImage] = [UIImage(named: "heart")!,UIImage(named: "camera")!,UIImage(named: "cameraRoll")!]

var items: [String] = ["Item 1", "Item 2", "Item 3"]
{
didSet {
setupLabels()
}
}

var selectedIndex : Int = 0 {
didSet {
displayNewSelectedIndex()
}
}

@IBInspectable var selectedLabelColor : UIColor = UIColor.blackColor()
@IBInspectable var unselectedLabelColor : UIColor = UIColor.grayColor()


@IBInspectable var borderColor : UIColor = UIColor.blackColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}

@IBInspectable var font : UIFont! = UIFont.systemFontOfSize(12) {
didSet {
setFont()
}
}

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

setupView()
}

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

func setupView(){


//layer.borderColor = UIColor(white: 1.0, alpha: 0.5).CGColor
layer.borderWidth = 1

backgroundColor = UIColor.clearColor()

setupLabels()

addIndividualItemConstraints(labels, mainView: self, padding: 0)


}







func setupLabels(){

for label in labels {
label.removeFromSuperview()
}

labels.removeAll(keepCapacity: true)

for index in 1...items.count {

let label = UILabel(frame: CGRectMake(0, 0, 70, 40))
label.text = items[index - 1]
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.font = UIFont(name: "Avenir-Black", size: 15)
label.textColor = index == 1 ? unselectedLabelColor : unselectedLabelColor
label.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(label)
labels.append(label)
}

addIndividualItemConstraints(labels, mainView: self, padding: 0)
}









override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {

let location = touch.locationInView(self)

var calculatedIndex : Int?
for (index, item) in labels.enumerate() {
if item.frame.contains(location) {
calculatedIndex = index
}
}


if calculatedIndex != nil {
selectedIndex = calculatedIndex!
sendActionsForControlEvents(.ValueChanged)
}

return false
}

func displayNewSelectedIndex(){
for (index, item) in labels.enumerate() {
item.textColor = unselectedLabelColor
}

let label = labels[selectedIndex]
label.textColor = selectedLabelColor

UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [], animations: {


}, completion: nil)
}

func addIndividualItemConstraints(items: [UIView], mainView: UIView, padding: CGFloat) {

let constraints = mainView.constraints

for (index, button) in items.enumerate() {

let topConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)

let bottomConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)

var rightConstraint : NSLayoutConstraint!

if index == items.count - 1 {

rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: -padding)

}else{

let nextButton = items[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: nextButton, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: -padding)
}


var leftConstraint : NSLayoutConstraint!

if index == 0 {

leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: padding)

}else{

let prevButton = items[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: prevButton, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: padding)

let firstItem = items[0]

let widthConstraint = NSLayoutConstraint(item: button, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: firstItem, attribute: .Width, multiplier: 1.0 , constant: 0)

mainView.addConstraint(widthConstraint)
}

mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}

func setSelectedColors(){
for item in labels {
item.textColor = unselectedLabelColor
}

if labels.count > 0 {
labels[0].textColor = unselectedLabelColor
}


}

func setFont(){
for item in labels {
item.font = font
}
}


}

如果有人可以提供帮助,我将非常感激!

致以诚挚的问候。

最佳答案

您可以有一个存储图像名称的字符串数组。例如

var images: [String] = ["image1.png", "image2.png", "image"]

在 setupLabels 中,您可以使用以下命令创建图像

var img = UIImage(named: images[index - 1])
let imageView = UIImageView(image: img!)
imageView.frame = CGRectMake(0, 0, 70, 40)
self.addSubview(imageView)

关于ios - 如何将分段 Controller 按钮从 UILabel 更改为 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063987/

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