【问题标题】:UIButton `setTitleShadowColor:ForState` not working as expectedUIButton `setTitleShadowColor:ForState` 没有按预期工作
【发布时间】:2015-09-25 02:19:39
【问题描述】:

我有一个带有 CustomCollectionViewCell 类的 UICollectionView。 在单元格内,我有一个使用情节提要设置的按钮。我希望按钮在点击时更改其颜色,但我一直在选择的标题的阴影之类的情况下,在选择时的背景下的背景。

我尝试用setTitleShadowColor:ForState 和.Selected 和.Normal 修复它。但它似乎没有做任何事情。

视图控制器:

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

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

【问题讨论】:

    标签: ios swift uibutton swift2


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2020-03-18
      • 2012-06-14
      • 2014-11-15
      • 1970-01-01
      • 2012-07-02
      • 2011-09-07
      • 2013-03-03
      • 2015-05-18
      相关资源
      最近更新 更多