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

标签 ios swift uibutton swift2

我有一个带有 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/

相关文章:

ios - swift : Issue with saving contact with AddressBook - (Xcode6-Beta5)

ios - 按钮不会取消隐藏

objective-c - 按下按钮时如何禁用按钮数组中的其余按钮

ios - 不支持armv6设备的XCode 4.5有什么后果?

ios - 如何将 View 宽度和高度转换为 CGpoint 坐标?

android - Ionic 2-无法使用 aar 文件/ios 框架添加自定义 android/ios 插件

ios - 如何在应用程序快速启动时取消选择 uitableview 单元格

swift - 你如何在不使用 UIKit 的情况下处理对 SKSpriteNode 的点击

iOS - 如何在不初始化 Swift 类的情况下调用方法?

ios - 将 subview 保留为 UIStackView 中的原始尺寸