ios - How do I put a UIActivityIndicatorView in a UIAlertController? -
we're moving away mbprogresshud because it's glitchy in our app, , doesn't have features such blocking user input or providing cancel button.
so, i've attempted implement swipesight's how display activity indicator in center of uialertcontroller?, , ran improper positioning of indicator:
it green because our app's tint green.
as can see, it's not in white rectangle part of controller, grey background. uses similar "@62shark" solution:
// in implementation: @property (nonatomic, strong) uiactivityindicatorview *spinner; @property (nonatomic, strong) uialertcontroller *alertcontroller; // in init: _alertcontroller = [uialertcontroller alertcontrollerwithtitle: @"loading" message: nil preferredstyle: uialertcontrollerstylealert]; _spinner = [uiactivityindicatorview new]; _spinner.translatesautoresizingmaskintoconstraints = false; _spinner.userinteractionenabled = false; _spinner.color = [themingassistant tintcolor]; _spinner.frame = _alertcontroller.view.bounds; _spinner.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight; [_spinner startanimating]; [_alertcontroller.view addsubview: _spinner]; // ... - (void) showon: (uiviewcontroller *)target title: (nsstring *)title message: (nsstring *)message cancancel: (bool)cancancel { self.alertcontroller.title = title; self.alertcontroller.message = message; if (cancancel) { [self.alertcontroller addaction:[uialertaction actionwithtitle: @"cancel" style: uialertactionstylecancel handler: ^(uialertaction *name){ [self customdismiss]; }]]; } nsdictionary *views = @{@"pending" : self.alertcontroller.view, @"indicator" : self.spinner}; nsarray *constraints = [nslayoutconstraint constraintswithvisualformat: @"v:[indicator]-(-50)-|" options: 0 metrics: nil views: views]; [constraints arraybyaddingobjectsfromarray: [nslayoutconstraint constraintswithvisualformat: @"h:|[indicator]|" options: 0 metrics: nil views: views]]; [target presentviewcontroller: self.alertcontroller animated: true completion: nil]; }
even if in white rectangle, fear might run text (also, when in there, want in middle-top, mbprogresshud does), i'll need way reserve space it.
so, question two-fold: how reserve space uiactivityindicatorview
in uialertcontroller
's white rectangle, , how place in there?
like jonasg mentioned here there property named contentviewcontroller
, can use kvc access
example :
uiviewcontroller *v = [[uiviewcontroller alloc] init]; v.view.backgroundcolor = [uicolor redcolor]; [alertcontroller setvalue:v forkey:@"contentviewcontroller"];
so here how code should looks (tested , works fine) :
- (ibaction)buttonclicked:(id)sender { self.alertcontroller = [uialertcontroller alertcontrollerwithtitle: @"loading" message: nil preferredstyle: uialertcontrollerstylealert]; [self.alertcontroller addaction:[uialertaction actionwithtitle: @"cancel" style: uialertactionstylecancel handler:nil]]; uiviewcontroller *customvc = [[uiviewcontroller alloc] init]; uiactivityindicatorview* spinner = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; [spinner startanimating]; [customvc.view addsubview:spinner]; [customvc.view addconstraint:[nslayoutconstraint constraintwithitem: spinner attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:customvc.view attribute:nslayoutattributecenterx multiplier:1.0f constant:0.0f]]; [customvc.view addconstraint:[nslayoutconstraint constraintwithitem: spinner attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toitem:customvc.view attribute:nslayoutattributecentery multiplier:1.0f constant:0.0f]]; [self.alertcontroller setvalue:customvc forkey:@"contentviewcontroller"]; [self presentviewcontroller: self.alertcontroller animated: true completion: nil]; }
you can override
-preferredcontentsize
return custom size in view controller settingcontentviewcontroller
.
in our case it's customvc
result:
want text below indicator ?
i have created uiviewcontroller
xib
act custom controller our contentviewcontroller
in first example have created view controller without xib file, can add views using interface builder, have added activity indicator , set constraints centered horizontally , vertically , label under activity indicator centered horizontally here interface builder:
we have less code now:
- (ibaction)buttonclicked:(id)sender { self.alertcontroller = [uialertcontroller alertcontrollerwithtitle: nil message: nil preferredstyle: uialertcontrollerstylealert]; mycustomviewcontroller *v = [[mycustomviewcontroller alloc] init]; [self.alertcontroller setvalue:v forkey:@"contentviewcontroller"]; [self presentviewcontroller: self.alertcontroller animated: true completion: nil]; }
result :
Comments
Post a Comment