【问题标题】:Trying to change UIButton title color and frame on scrollview scroll尝试在滚动视图滚动上更改 UIButton 标题颜色和框架
【发布时间】:2019-10-07 03:45:35
【问题描述】:

当我将滚动视图滚动到最后一页时,我正在尝试更改 UIButton 的标题颜色以及按钮的框架。 UIButton 在滚动视图之外。

如果我只更改框架,它可以工作,如果我只更改标题颜色,它可以工作,但是当我尝试更改这两个框架时,框架不会改变。

我要更改的按钮是 signupButton

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let pageNum = Int(scrollView.contentOffset.x / scrollView.frame.size.width)

    if(pageNum == 2) {
        UIView.animate(withDuration: 0.3) {
            self.nextButton.layer.opacity = 0.0
            self.signupButton.backgroundColor = UIColor(red: 75/255, green: 159/255, blue: 186/255, alpha: 1.0)
            self.signupButton.frame.size = CGSize(width: UIScreen.main.bounds.width - 69,height: 45)
            self.signupButton.setTitleColor(.white, for: .normal)
        }

    } else {
        UIView.animate(withDuration: 0.3) {
            self.nextButton.layer.opacity = 1.0
            self.signupButton.backgroundColor = .clear
            self.signupButton.setTitleColor(UIColor(red: 75/255, green: 159/255, blue: 186/255, alpha: 1.0), for: .normal)
        }
    }

    UIView.animate(withDuration: 0.2) {
        self.pageControl.currentPage = pageNum
    }
}

我希望标题颜色和框架都会改变,但是当我同时做这两个时,只有颜色会改变。看来UIButton.setTitleColor() 干扰了框架的变化。

【问题讨论】:

    标签: ios swift uiscrollview uibutton


    【解决方案1】:

    不要使用setTitleColor(_:for:)方法设置UIButton'stitleColor,而是使用按钮的titleLabel更改color,即

    UIView.animate(withDuration: 0.3) {
        self.nextButton.layer.opacity = 0.0
        self.signupButton.backgroundColor = UIColor(red: 75/255, green: 159/255, blue: 186/255, alpha: 1.0)
        self.signupButton.frame.size = CGSize(width: UIScreen.main.bounds.width - 69,height: 45)
        self.signupButton.titleLabel?.textColor = .white
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多