Ios 自定义加载指示器不显示';不显示在UIButton中

Ios 自定义加载指示器不显示';不显示在UIButton中,ios,swift,uikit,Ios,Swift,Uikit,我设计了一个自定义动画作为我的活动指示器: class LoadingIndicator: UIView { override init(frame: CGRect) { super.init(frame: frame) } required init?(coder: NSCoder) { super.init(coder: coder) } func startLoadingIndicator() { //Animation Logic } func stop

我设计了一个自定义动画作为我的活动指示器:

class LoadingIndicator: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)

}

func startLoadingIndicator() {
    //Animation Logic
}

func stopLoadingIndicator() {
    //Stop and hide animation
}
}
该类在“我的启动视图”控制器中用作故事板自定义类,并正确显示动画

现在,我想在我的提交按钮上添加相同的类,以便在等待网络响应时显示动画

我已将这些函数添加到SubmitButton类中:

class SubmitButton: UIButton {

var originalButtonText: String?
var loadingIndicator: LoadingIndicator!

required init?(coder: NSCoder) {
    ...
}


...

//I call this method in my view controller:

func showLoading() {
    originalButtonText = self.titleLabel?.text
    self.setTitle("", for: .normal)

    if (loadingIndicator == nil) {
        loadingIndicator = initLoadingIndicator()
    }

    showAnimating()
}

func hideLoading() {
    ...
}

private func initLoadingIndicator() -> LoadingIndicator {
    let loadingIndicator = LoadingIndicator()
    return loadingIndicator
}

private func showAnimating() {
    loadingIndicator.translatesAutoresizingMaskIntoConstraints = false

    self.addSubview(loadingIndicator)
    centerLoadingIndicatorInButton()
    loadingIndicator.startLoadingIndicator()
}

private func centerLoadingIndicatorInButton() {
    let xCenterConstraint = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: loadingIndicator, attribute: .centerX, multiplier: 1, constant: 0)
    self.addConstraint(xCenterConstraint)

    let yCenterConstraint = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: loadingIndicator, attribute: .centerY, multiplier: 1, constant: 0)
    self.addConstraint(yCenterConstraint)
}
}
但是我看不到我的动画。我尝试添加一个UIActivityIndicatorView和一个简单的矩形视图,它们都出现在我的按钮上。因此,LoadingIndicator类或如何初始化它应该有问题。但我找不到什么。即使没有动画,我也无法将LoadingIndicator视图放在按钮内。我试着给它一个背景色,但它什么都没有。我能试试什么

解决方案

有两个问题:

  • 因为我已经使用了
    translatesAutoResizengMaskintoConstraints
    ,所以宽度和高度锚定为零,我应该将它们定义为约束:

    让约束=[ 加载指示器.宽度锚点.约束(相等常数:34), 加载指示器。高度锚定。约束(相等常量:10), 加载指示器.centerXAnchor.constraint(等于:self.centerXAnchor), 加载指示器.centerYAnchor.constraint(等式:self.centerYAnchor) ]

  • 我已经在
    viewdiload
    中调用了动画方法,我尝试了
    viewwillreased
    ,现在我终于可以看到动画了


  • 您的代码看起来不错,但要获得帮助,您可以参考以下内容-在运行时使用Xcode的视图调试功能捕获视图层次结构。这将显示您在视觉上和作为对象列表的视图。检查您的对象是否在列表中。如果是,请查找0维度或屏幕外位置。如果您向我们展示了
    startLoadingIndicator
    方法的代码,可能会有所帮助。@PhillipMills谢谢,我不知道此功能,它帮助我找到了一个问题。因为我使用了
    translatesAutoresizingMaskIntoConstraints
    ,所以我的视图大小为零。@Pierce因为它在其他地方工作,所以我认为它应该是无关的。感谢您的评论。您的代码看起来不错,但要获得帮助,您可以参考此内容-在运行时使用Xcode的视图调试功能捕获视图层次结构。这将显示您在视觉上和作为对象列表的视图。检查您的对象是否在列表中。如果是,请查找0维度或屏幕外位置。如果您向我们展示了
    startLoadingIndicator
    方法的代码,可能会有所帮助。@PhillipMills谢谢,我不知道此功能,它帮助我找到了一个问题。因为我使用了
    translatesAutoresizingMaskIntoConstraints
    ,所以我的视图大小为零。@Pierce因为它在其他地方工作,所以我认为它应该是无关的。谢谢你的评论。
    NSLayoutConstraint.activate(constraints)