Place a UIActivityIndicator inside a UIButton Place a UIActivityIndicator inside a UIButton ios ios

Place a UIActivityIndicator inside a UIButton


I think the concept you are missing is that a view's frame (and its center) are both in relation to its superview. Based on your screenshot I would guess that your textfields and buttons are all in a view that is acting as a container. So your button's frame & center are in relation to that container view and not to the view controller's view as a whole. You assign the same frame & center to the activity indicator, but then you add the indicator as a subview of the main view and not the container view. So you now have two views (the button and the indicator) with the same frame, but that frame is in relation to two different superviews.

The easiest change would just be to add your indicator to the container view you are using. But I would suggest adding the indicator as a subview of the button, then just do a little math to tweak its position.

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];

As a side note: Your setting of the center of a view just after the frame of the view is redundant. Also, the last view added as a subview is automatically the front subview.


Based on @Musa almatri answer, I create extension:

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()        }    }}}

then you can use it like this:

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


Here is a Swift 3 version, without using tags.

import UIKitextension 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()                }            }        }    }}