web-dev-qa-db-ja.com

UIButton内にUIActivityIndi​​catorを配置します

私は愚かな何かが欠けていることを知っていますが、とにかく、ここに私のコードがあります:

UIActivityIndicatorView indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.hidesWhenStopped = YES;
indicator.frame = btnLogin.frame;
indicator.center = btnLogin.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:indicator];

最終結果は次のとおりです。

Screenshot of misplaced UIActivityIndicator

http://img542.imageshack.us/img542/8172/uiactivity.png

前もって感謝します!

17
Matthys Du Toit

あなたが見逃している概念は、ビューのフレーム(およびその中心)が両方ともそのスーパービューに関連しているということだと思います。あなたのスクリーンショットに基づいて、私はあなたのテキストフィールドとボタンがすべてコンテナとして機能しているビューにあると思います。したがって、ボタンのフレームと中心は、ビューコントローラのビュー全体ではなく、そのコンテナビューに関連しています。同じフレームと中心をアクティビティ区分に割り当てますが、次に区分をコンテナビューではなくメインビューのサブビューとして追加します。これで、同じフレームを持つ2つのビュー(ボタンとインジケーター)ができましたが、そのフレームは2つの異なるスーパービューに関連しています。

最も簡単な変更は、使用しているコンテナビューにインジケータを追加することです。ただし、ボタンのサブビューとしてインジケーターを追加してから、少し計算してその位置を微調整することをお勧めします。

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGFloat halfButtonHeight = btnLogin.bounds.size.height / 2;
CGFloat buttonWidth = btnLogin.bounds.size.width;
indicator.center = CGPointMake(buttonWidth - halfButtonHeight , halfButtonHeight);
[btnLogin addSubview:indicator];
[indicator startAnimating];

補足として:ビューのフレームの直後のビューの中心の設定は冗長です。また、サブビューとして追加された最後のビューは、自動的にフロントサブビューになります。

31
NJones

@Musa almatriの回答に基づいて、拡張機能を作成します。

extension UIButton {
func loadingIndicator(show show: Bool) {
    let tag = 9876
    if show {
        let indicator = UIActivityIndicatorView()
        let buttonHeight = self.bounds.size.height
        let buttonWidth = self.bounds.size.width
        indicator.center = CGPointMake(buttonWidth/2, buttonHeight/2)
        indicator.tag = tag
        self.addSubview(indicator)
        indicator.startAnimating()
    } else {
        if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
            indicator.stopAnimating()
            indicator.removeFromSuperview()
        }
    }
}}

次に、次のように使用できます。

yourButton.loadingIndicator(show: true) //hide -> show: false
21
Daniel Kuta

タグを使用しないSwift 3バージョンです。

import UIKit

extension UIButton {
    func loadingIndicator(show: Bool) {
        if show {
            let indicator = UIActivityIndicatorView()
            let buttonHeight = self.bounds.size.height
            let buttonWidth = self.bounds.size.width
            indicator.center = CGPoint(x: buttonWidth/2, y: buttonHeight/2)
            self.addSubview(indicator)
            indicator.startAnimating()
        } else {
            for view in self.subviews {
                if let indicator = view as? UIActivityIndicatorView {
                    indicator.stopAnimating()
                    indicator.removeFromSuperview()
                }
            }
        }
    }
}
9
Mike Mikina

少しアップグレード:(スーパービューにボタンを追加)

extension UIButton {
    func loadingIndicator(_ show: Bool) {
        let indicatorTag = 808404
        if show {
            isEnabled = false
            alpha = 0
            let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
            indicator.center = center
            indicator.tag = indicatorTag
            superview?.addSubview(indicator)
            indicator.startAnimating()
        } else {
            isEnabled = true
            alpha = 1.0
            if let indicator = superview?.viewWithTag(indicatorTag) as? UIActivityIndicatorView {
                indicator.stopAnimating()
                indicator.removeFromSuperview()
            }
        }
    }
}
3
Artur Guseynov

Swift Solution

var indicator = UIActivityIndicatorView()
var halfButtonHeight = SOME_BUTTON.bounds.size.height / 2;
var buttonWidth = SOME_BUTTON.bounds.size.width;
indicator.center = CGPointMake(buttonWidth - halfButtonHeight , halfButtonHeight);
SOME_BUTTON.addSubview(indicator)
indicator.startAnimating()

そしてボタンの中央にそれを作るために

indicator.center = CGPointMake(buttonWidth/2, halfButtonHeight);

または優れたライブラリを使用する

https://github.com/souzainf3/RNLoadingButton-Swift

3
Musa almatri

制約を使用して、UIButton内のインジケーターを中央に配置しています。 @DanielQの拡張子を適応させると、次のようになります。

extension UIButton {
    func loadingIndicator(show: Bool) {
        let tag = 9876
        if show {
            let indicator = UIActivityIndicatorView()
            indicator.tag = tag
            self.addSubview(indicator)
            indicator.translatesAutoresizingMaskIntoConstraints = false
            let horizontalConstraint = NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
            let verticalConstraint = NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
            self.addConstraints([horizontalConstraint, verticalConstraint])
            indicator.startAnimating()
        } else {
            if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
                indicator.stopAnimating()
                indicator.removeFromSuperview()
            }
        }
    }
}
2
Sam

ボタンからタイトルを削除し、アニメーションが終了したら、インジケーターがタイトルをオーバーライドするのではなく、タイトルを追加し直しました。

extension UIButton {

func loadingIndicator(show: Bool) {
    let tag = 9876

    var color: UIColor?

    if show {
        color = titleColor(for: .normal)
        let indicator = UIActivityIndicatorView()
        let buttonHeight = self.bounds.size.height
        let buttonWidth = self.bounds.size.width
        indicator.center = CGPoint(x: buttonWidth/2, y: buttonHeight/2)
        indicator.tag = tag
        indicator.color = UIColor.white
        setTitleColor(.clear, for: .normal)

        self.addSubview(indicator)
        indicator.startAnimating()
    } else {
        if let indicator = self.viewWithTag(tag) as? UIActivityIndicatorView {
            indicator.stopAnimating()
            indicator.removeFromSuperview()
            setTitleColor(color, for: .normal)
        }
    }
}
}
0
luke