2013-03-07 17 views
13

Wiem, że brakuje czegoś głupiego, ale w każdym razie, tutaj jest mój kod:Zamieścić UIActivityIndicator wewnątrz UIButton

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

Oto efekt końcowy:

Screenshot of misplaced UIActivityIndicator

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

Z góry dziękuję!

+0

Ktoś? Naprawdę nie chwytam pozycjonowania w systemie iOS. –

Odpowiedz

29

Uważam, że brakującą koncepcją jest to, że ramka widoku (i jego środek) są związane z jego widokiem. Na podstawie zrzutu ekranu domyślam się, że twoje pola tekstowe i przyciski są w widoku, który działa jako kontener. Tak więc środek ramki twojego przycisku jest związany z tym widokiem kontenera, a nie z widokiem kontrolera widoku jako całości. Przypisujesz tę samą ramkę centrum do centrum aktywności, ale dodajesz wskaźnik jako widok główny widoku głównego, a nie widok kontenera. Masz teraz dwa widoki (przycisk i wskaźnik) z tą samą ramką, ale ta ramka jest powiązana z dwoma różnymi widokami.

Najprostszą zmianą byłoby dodanie wskaźnika do widoku kontenera, którego używasz. Ale sugerowałbym dodanie wskaźnika jako podglądu przycisku, a następnie zrób trochę matematyki, aby poprawić jego pozycję.

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

Uwaga: Twoje ustawienie środka widoku tuż za ramką widoku jest zbędne. Ponadto ostatni widok dodany jako widok podrzędny jest automatycznie przednim podglądem.

3

Swift Rozwiązanie:

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

I zrobić to w środku przycisku

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

lub użyć wielkiej biblioteki

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

15

podstawie @Musa almatri odpowiedź, tworzę rozszerzenia:

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

wtedy można go używać tak:

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

Uwaga spowoduje to umieszczenie wskaźnika na środku przycisku. Zaakceptowana odpowiedź z 'CGPointMake (buttonWidth - halfButtonHeight, halfButtonHeight)' umieszcza go po prawej stronie (w prosty, sprytny sposób, IMO). – dbreaux

2

Używam contraints aby wyśrodkować wskaźnik wewnątrz UIButton. Adaptacja rozszerzenie @DanielQ, że staje się:

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

Oto wersja Swift 3, bez używania znaczników.

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

usunąłem tytuł z przycisku Następnie dodawano ją z powrotem, gdy animacja zakończył zamiast wskaźnika nadrzędnymi tytułem:

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

Mała aktualizacja: (dodanie przycisku na SuperView)

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