gpt4 book ai didi

ios - UIButton `setTitleShadowColor:ForState` 未按预期工作

转载 作者:行者123 更新时间:2023-11-30 14:02:51 24 4
gpt4 key购买 nike

我有一个带有 CustomCollectionViewCell 类的 UICollectionView。在单元格内,我有一个用 Storyboard设置的按钮。我希望按钮在点击时改变其颜色,但我总是得到类似标题阴影的东西,其蓝色与选择时的背景颜色不同。

我尝试使用 setTitleShadowColor:ForState 以及 .Selected.Normal 修复此问题。但它似乎没有做任何事情。

View Controller :

...
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfButtons
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCollectionViewCell
cell.setContents(indexPath.row)
return cell
}
...

自定义CollectionViewCell:

...
func setContents(row: Int) {
buttonInCell.setTitle(String(row + 1), forState: .Normal)
buttonInCell.setTitleColor(UIColor.blueColor(), forState: .Normal)
buttonInCell.layer.backgroundColor = UIColor.whiteColor().CGColor
buttonInCell.layer.cornerRadius = buttonRadius
buttonInCell.layer.borderColor = UIColor.blueColor().CGColor
buttonInCell.layer.borderWidth = buttonBorderWidth
}

func changeButtonStatus(sender: UIButton) {
if sender.selected {
sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
sender.layer.backgroundColor = UIColor.whiteColor().CGColor
} else {
sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
sender.layer.backgroundColor = UIColor.blueColor().CGColor
}
}
// MARK: Actions

@IBAction func buttonInCellTapped(sender: UIButton) {
changeButtonStatus(sender)
sender.selected = !sender.selected
}
...

最佳答案

我刚刚找到了一个解决方案,只需在 Xcode 的属性检查器中将按钮类型更改为“自定义”即可。

关于ios - UIButton `setTitleShadowColor:ForState` 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773765/

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