尝试更改UIButton标题的颜色和scrollview滚动上的框架

时间:2019-05-20 18:49:28

标签: ios swift uiscrollview uibutton

当我将滚动视图滚动到最后一页时,我试图更改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()会干扰帧的更改。

1 个答案:

答案 0 :(得分:0)

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

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
}