BarButtonItemWithActivity.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "BarButtonItemWithActivity.h"
  6. #import "NCAppBranding.h"
  7. @interface BarButtonItemWithActivity ()
  8. @end
  9. @implementation BarButtonItemWithActivity
  10. - (instancetype)init {
  11. return [super init];
  12. }
  13. - (instancetype)initWithWidth:(CGFloat)buttonWidth withImage:(UIImage *)buttonImage {
  14. self = [self init];
  15. if (self) {
  16. UIColor *themeTextColor = [NCAppBranding themeTextColor];
  17. // Use UIButton as CustomView in UIBarButtonItem to have a fixed-size item
  18. self.innerButton = [[UIButton alloc] init];
  19. [self.innerButton setImage:buttonImage forState:UIControlStateNormal];
  20. self.innerButton.frame = CGRectMake(0, 0, buttonWidth, buttonWidth);
  21. self.innerButton.tintColor = themeTextColor;
  22. // Make sure the size of UIBarButtonItem stays the same when displaying the ActivityIndicator
  23. self.activityIndicator = [[UIActivityIndicatorView alloc] init];
  24. self.activityIndicator.color = themeTextColor;
  25. self.activityIndicator.frame = CGRectMake(0, 0, buttonWidth, buttonWidth);
  26. [self setCustomView:self.innerButton];
  27. }
  28. return self;
  29. }
  30. - (void)showActivityIndicator
  31. {
  32. [self.activityIndicator startAnimating];
  33. [self setCustomView:self.activityIndicator];
  34. }
  35. - (void)hideActivityIndicator
  36. {
  37. [self setCustomView:self.innerButton];
  38. [self.activityIndicator stopAnimating];
  39. }
  40. @end