简体   繁体   English

UIButton`setTitleShadowColor:ForState`无法正常工作

[英]UIButton `setTitleShadowColor:ForState` not working as expected

I have a UICollectionView with a CustomCollectionViewCell class. 我有一个带有CustomCollectionViewCell类的UICollectionView。 Inside the cell I have a button I did set with storyboard. 在单元中,我有一个设置了情节提要的按钮。 I want the button to change its color when tapped, but I keep getting something like the shadow of the title with a different blue color than the background when selected. 我希望按钮在被点击时能够改变其颜色,但是当选择时,我会不断获得类似标题阴影的蓝色与背景不同的颜色。

I've tried fixing it with setTitleShadowColor:ForState with .Selected and .Normal . 我尝试使用setTitleShadowColor:ForState.Selected.Normal修复它。 But it doesn't seems to do anything. 但这似乎无能为力。

ViewController: ViewController:

...
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
}
...

CustomCollectionViewCell: CustomCollectionViewCell:

...
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的属性检查器中将按钮类型更改为“ custom”即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM