Açıklama Yok

LGAlertView.m 159KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368
  1. //
  2. // LGAlertView.m
  3. // LGAlertView
  4. //
  5. //
  6. // The MIT License (MIT)
  7. //
  8. // Copyright © 2015 Grigory Lutkov <Friend.LGA@gmail.com>
  9. // (https://github.com/Friend-LGA/LGAlertView)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in all
  19. // copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28. //
  29. #import "LGAlertView.h"
  30. #import "LGAlertViewWindow.h"
  31. #import "LGAlertViewController.h"
  32. #import "LGAlertViewCell.h"
  33. #import "LGAlertViewTextField.h"
  34. #import "LGAlertViewButton.h"
  35. #import "LGAlertViewHelper.h"
  36. #import "LGAlertViewWindowsObserver.h"
  37. #import "LGAlertViewShadowView.h"
  38. #pragma mark - Constants
  39. NSString * _Nonnull const LGAlertViewWillShowNotification = @"LGAlertViewWillShowNotification";
  40. NSString * _Nonnull const LGAlertViewDidShowNotification = @"LGAlertViewDidShowNotification";
  41. NSString * _Nonnull const LGAlertViewWillDismissNotification = @"LGAlertViewWillDismissNotification";
  42. NSString * _Nonnull const LGAlertViewDidDismissNotification = @"LGAlertViewDidDismissNotification";
  43. NSString * _Nonnull const LGAlertViewActionNotification = @"LGAlertViewActionNotification";
  44. NSString * _Nonnull const LGAlertViewCancelNotification = @"LGAlertViewCancelNotification";
  45. NSString * _Nonnull const LGAlertViewDestructiveNotification = @"LGAlertViewDestructiveNotification";
  46. NSString * _Nonnull const LGAlertViewDidDismissAfterActionNotification = @"LGAlertViewDidDismissAfterActionNotification";
  47. NSString * _Nonnull const LGAlertViewDidDismissAfterCancelNotification = @"LGAlertViewDidDismissAfterCancelNotification";
  48. NSString * _Nonnull const LGAlertViewDidDismissAfterDestructiveNotification = @"LGAlertViewDidDismissAfterDestructiveNotification";
  49. NSString * _Nonnull const LGAlertViewShowAnimationsNotification = @"LGAlertViewShowAnimationsNotification";
  50. NSString * _Nonnull const LGAlertViewDismissAnimationsNotification = @"LGAlertViewDismissAnimationsNotification";
  51. NSString * _Nonnull const kLGAlertViewAnimationDuration = @"duration";
  52. #pragma mark - Types
  53. typedef enum {
  54. LGAlertViewTypeDefault = 0,
  55. LGAlertViewTypeActivityIndicator = 1,
  56. LGAlertViewTypeProgressView = 2,
  57. LGAlertViewTypeTextFields = 3
  58. }
  59. LGAlertViewType;
  60. #pragma mark - Interface
  61. @interface LGAlertView () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIGestureRecognizerDelegate>
  62. @property (readwrite) BOOL showing;
  63. @property (readwrite) LGAlertViewStyle style;
  64. @property (readwrite) NSString *title;
  65. @property (readwrite) NSString *message;
  66. @property (readwrite) UIView *innerView;
  67. @property (readwrite) NSArray *buttonTitles;
  68. @property (readwrite) NSString *cancelButtonTitle;
  69. @property (readwrite) NSString *destructiveButtonTitle;
  70. @property (readwrite) NSArray *textFieldsArray;
  71. @property (assign, nonatomic, getter=isExists) BOOL exists;
  72. @property (strong, nonatomic) LGAlertViewWindow *window;
  73. @property (strong, nonatomic) UIView *view;
  74. @property (strong, nonatomic) LGAlertViewController *viewController;
  75. @property (strong, nonatomic) UIVisualEffectView *backgroundView;
  76. @property (strong, nonatomic) LGAlertViewShadowView *shadowView;
  77. @property (strong, nonatomic) LGAlertViewShadowView *shadowCancelView;
  78. @property (strong, nonatomic) UIVisualEffectView *blurView;
  79. @property (strong, nonatomic) UIVisualEffectView *blurCancelView;
  80. @property (strong, nonatomic) UIScrollView *scrollView;
  81. @property (strong, nonatomic) UITableView *tableView;
  82. @property (strong, nonatomic) UILabel *titleLabel;
  83. @property (strong, nonatomic) UILabel *messageLabel;
  84. @property (strong, nonatomic) UIView *innerContainerView;
  85. @property (strong, nonatomic) LGAlertViewButton *destructiveButton;
  86. @property (strong, nonatomic) LGAlertViewButton *cancelButton;
  87. @property (strong, nonatomic) LGAlertViewButton *firstButton;
  88. @property (strong, nonatomic) LGAlertViewButton *secondButton;
  89. @property (strong, nonatomic) LGAlertViewButton *thirdButton;
  90. @property (strong, nonatomic) UIView *separatorHorizontalView;
  91. @property (strong, nonatomic) UIView *separatorVerticalView1;
  92. @property (strong, nonatomic) UIView *separatorVerticalView2;
  93. @property (assign, nonatomic) CGPoint scrollViewCenterShowed;
  94. @property (assign, nonatomic) CGPoint scrollViewCenterHidden;
  95. @property (assign, nonatomic) CGPoint cancelButtonCenterShowed;
  96. @property (assign, nonatomic) CGPoint cancelButtonCenterHidden;
  97. @property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
  98. @property (strong, nonatomic) UIProgressView *progressView;
  99. @property (strong, nonatomic) UILabel *progressLabel;
  100. @property (assign, nonatomic) LGAlertViewType type;
  101. @property (assign, nonatomic) CGFloat keyboardHeight;
  102. @property (strong, nonatomic) NSMutableDictionary *buttonsPropertiesDictionary;
  103. @property (strong, nonatomic) NSMutableArray *buttonsEnabledArray;
  104. @property (strong, nonatomic) LGAlertViewCell *heightCell;
  105. @property (assign, nonatomic) NSUInteger numberOfTextFields;
  106. @property (copy, nonatomic) LGAlertViewTextFieldsSetupHandler textFieldsSetupHandler;
  107. @property (assign, nonatomic, getter=isUserCancelOnTouch) BOOL userCancelOnTouch;
  108. @property (assign, nonatomic, getter=isUserButtonsHeight) BOOL userButtonsHeight;
  109. @property (assign, nonatomic, getter=isUserTitleTextColor) BOOL userTitleTextColor;
  110. @property (assign, nonatomic, getter=isUserTitleFont) BOOL userTitleFont;
  111. @property (assign, nonatomic, getter=isUserMessageTextColor) BOOL userMessageTextColor;
  112. @property (assign, nonatomic, getter=isUserButtonsTitleColor) BOOL userButtonsTitleColor;
  113. @property (assign, nonatomic, getter=isUserButtonsBackgroundColorHighlighted) BOOL userButtonsBackgroundColorHighlighted;
  114. @property (assign, nonatomic, getter=isUserCancelButtonTitleColor) BOOL userCancelButtonTitleColor;
  115. @property (assign, nonatomic, getter=isUserCancelButtonBackgroundColorHighlighted) BOOL userCancelButtonBackgroundColorHighlighted;
  116. @property (assign, nonatomic, getter=isUserActivityIndicatorViewColor) BOOL userActivityIndicatorViewColor;
  117. @property (assign, nonatomic, getter=isUserProgressViewProgressTintColor) BOOL userProgressViewProgressTintColor;
  118. @property (assign, nonatomic, getter=isInitialized) BOOL initialized;
  119. @end
  120. #pragma mark - Implementation
  121. @implementation LGAlertView
  122. - (nonnull instancetype)initWithTitle:(nullable NSString *)title
  123. message:(nullable NSString *)message
  124. style:(LGAlertViewStyle)style
  125. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  126. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  127. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  128. self = [super init];
  129. if (self) {
  130. self.style = style;
  131. self.title = title;
  132. self.message = message;
  133. self.buttonTitles = buttonTitles.mutableCopy;
  134. self.cancelButtonTitle = cancelButtonTitle;
  135. self.destructiveButtonTitle = destructiveButtonTitle;
  136. [self setupDefaults];
  137. }
  138. return self;
  139. }
  140. - (nonnull instancetype)initWithViewAndTitle:(nullable NSString *)title
  141. message:(nullable NSString *)message
  142. style:(LGAlertViewStyle)style
  143. view:(nullable UIView *)view
  144. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  145. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  146. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  147. self = [super init];
  148. if (self) {
  149. self.style = style;
  150. self.title = title;
  151. self.message = message;
  152. self.innerView = view;
  153. self.buttonTitles = buttonTitles.mutableCopy;
  154. self.cancelButtonTitle = cancelButtonTitle;
  155. self.destructiveButtonTitle = destructiveButtonTitle;
  156. [self setupDefaults];
  157. }
  158. return self;
  159. }
  160. - (nonnull instancetype)initWithActivityIndicatorAndTitle:(nullable NSString *)title
  161. message:(nullable NSString *)message
  162. style:(LGAlertViewStyle)style
  163. progressLabelText:(nullable NSString *)progressLabelText
  164. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  165. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  166. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  167. self = [super init];
  168. if (self) {
  169. self.style = style;
  170. self.type = LGAlertViewTypeActivityIndicator;
  171. self.title = title;
  172. self.message = message;
  173. self.buttonTitles = buttonTitles.mutableCopy;
  174. self.cancelButtonTitle = cancelButtonTitle;
  175. self.destructiveButtonTitle = destructiveButtonTitle;
  176. self.progressLabelText = progressLabelText;
  177. [self setupDefaults];
  178. }
  179. return self;
  180. }
  181. - (nonnull instancetype)initWithProgressViewAndTitle:(nullable NSString *)title
  182. message:(nullable NSString *)message
  183. style:(LGAlertViewStyle)style
  184. progress:(float)progress
  185. progressLabelText:(NSString *)progressLabelText
  186. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  187. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  188. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  189. self = [super init];
  190. if (self) {
  191. self.style = style;
  192. self.type = LGAlertViewTypeProgressView;
  193. self.title = title;
  194. self.message = message;
  195. self.buttonTitles = buttonTitles.mutableCopy;
  196. self.cancelButtonTitle = cancelButtonTitle;
  197. self.destructiveButtonTitle = destructiveButtonTitle;
  198. self.progress = progress;
  199. self.progressLabelText = progressLabelText;
  200. [self setupDefaults];
  201. }
  202. return self;
  203. }
  204. - (nonnull instancetype)initWithTextFieldsAndTitle:(nullable NSString *)title
  205. message:(nullable NSString *)message
  206. numberOfTextFields:(NSUInteger)numberOfTextFields
  207. textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
  208. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  209. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  210. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  211. self = [super init];
  212. if (self) {
  213. self.style = LGAlertViewStyleAlert;
  214. self.type = LGAlertViewTypeTextFields;
  215. self.title = title;
  216. self.message = message;
  217. self.numberOfTextFields = numberOfTextFields;
  218. self.textFieldsSetupHandler = textFieldsSetupHandler;
  219. self.buttonTitles = buttonTitles.mutableCopy;
  220. self.cancelButtonTitle = cancelButtonTitle;
  221. self.destructiveButtonTitle = destructiveButtonTitle;
  222. [self setupDefaults];
  223. }
  224. return self;
  225. }
  226. + (nonnull instancetype)alertViewWithTitle:(nullable NSString *)title
  227. message:(nullable NSString *)message
  228. style:(LGAlertViewStyle)style
  229. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  230. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  231. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  232. return [[self alloc] initWithTitle:title
  233. message:message
  234. style:style
  235. buttonTitles:buttonTitles
  236. cancelButtonTitle:cancelButtonTitle
  237. destructiveButtonTitle:destructiveButtonTitle];
  238. }
  239. + (nonnull instancetype)alertViewWithViewAndTitle:(nullable NSString *)title
  240. message:(nullable NSString *)message
  241. style:(LGAlertViewStyle)style
  242. view:(nullable UIView *)view
  243. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  244. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  245. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  246. return [[self alloc] initWithViewAndTitle:title
  247. message:message
  248. style:style
  249. view:view
  250. buttonTitles:buttonTitles
  251. cancelButtonTitle:cancelButtonTitle
  252. destructiveButtonTitle:destructiveButtonTitle];
  253. }
  254. + (nonnull instancetype)alertViewWithActivityIndicatorAndTitle:(nullable NSString *)title
  255. message:(nullable NSString *)message
  256. style:(LGAlertViewStyle)style
  257. progressLabelText:(nullable NSString *)progressLabelText
  258. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  259. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  260. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  261. return [[self alloc] initWithActivityIndicatorAndTitle:title
  262. message:message
  263. style:style
  264. progressLabelText:progressLabelText
  265. buttonTitles:buttonTitles
  266. cancelButtonTitle:cancelButtonTitle
  267. destructiveButtonTitle:destructiveButtonTitle];
  268. }
  269. + (nonnull instancetype)alertViewWithProgressViewAndTitle:(nullable NSString *)title
  270. message:(nullable NSString *)message
  271. style:(LGAlertViewStyle)style
  272. progress:(float)progress
  273. progressLabelText:(NSString *)progressLabelText
  274. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  275. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  276. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  277. return [[self alloc] initWithProgressViewAndTitle:title
  278. message:message
  279. style:style
  280. progress:progress
  281. progressLabelText:progressLabelText
  282. buttonTitles:buttonTitles
  283. cancelButtonTitle:cancelButtonTitle
  284. destructiveButtonTitle:destructiveButtonTitle];
  285. }
  286. + (nonnull instancetype)alertViewWithTextFieldsAndTitle:(nullable NSString *)title
  287. message:(nullable NSString *)message
  288. numberOfTextFields:(NSUInteger)numberOfTextFields
  289. textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
  290. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  291. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  292. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle {
  293. return [[self alloc] initWithTextFieldsAndTitle:title
  294. message:message
  295. numberOfTextFields:numberOfTextFields
  296. textFieldsSetupHandler:textFieldsSetupHandler
  297. buttonTitles:buttonTitles
  298. cancelButtonTitle:cancelButtonTitle
  299. destructiveButtonTitle:destructiveButtonTitle];
  300. }
  301. #pragma mark -
  302. - (nonnull instancetype)initWithTitle:(nullable NSString *)title
  303. message:(nullable NSString *)message
  304. style:(LGAlertViewStyle)style
  305. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  306. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  307. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  308. actionHandler:(LGAlertViewActionHandler)actionHandler
  309. cancelHandler:(LGAlertViewHandler)cancelHandler
  310. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  311. self = [self initWithTitle:title
  312. message:message
  313. style:style
  314. buttonTitles:buttonTitles
  315. cancelButtonTitle:cancelButtonTitle
  316. destructiveButtonTitle:destructiveButtonTitle];
  317. if (self) {
  318. self.actionHandler = actionHandler;
  319. self.cancelHandler = cancelHandler;
  320. self.destructiveHandler = destructiveHandler;
  321. }
  322. return self;
  323. }
  324. - (nonnull instancetype)initWithViewAndTitle:(nullable NSString *)title
  325. message:(nullable NSString *)message
  326. style:(LGAlertViewStyle)style
  327. view:(nullable UIView *)view
  328. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  329. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  330. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  331. actionHandler:(LGAlertViewActionHandler)actionHandler
  332. cancelHandler:(LGAlertViewHandler)cancelHandler
  333. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  334. self = [self initWithViewAndTitle:title
  335. message:message
  336. style:style
  337. view:view
  338. buttonTitles:buttonTitles
  339. cancelButtonTitle:cancelButtonTitle
  340. destructiveButtonTitle:destructiveButtonTitle];
  341. if (self) {
  342. self.actionHandler = actionHandler;
  343. self.cancelHandler = cancelHandler;
  344. self.destructiveHandler = destructiveHandler;
  345. }
  346. return self;
  347. }
  348. - (nonnull instancetype)initWithActivityIndicatorAndTitle:(nullable NSString *)title
  349. message:(nullable NSString *)message
  350. style:(LGAlertViewStyle)style
  351. progressLabelText:(nullable NSString *)progressLabelText
  352. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  353. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  354. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  355. actionHandler:(LGAlertViewActionHandler)actionHandler
  356. cancelHandler:(LGAlertViewHandler)cancelHandler
  357. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  358. self = [self initWithActivityIndicatorAndTitle:title
  359. message:message
  360. style:style
  361. progressLabelText:progressLabelText
  362. buttonTitles:buttonTitles
  363. cancelButtonTitle:cancelButtonTitle
  364. destructiveButtonTitle:destructiveButtonTitle];
  365. if (self) {
  366. self.actionHandler = actionHandler;
  367. self.cancelHandler = cancelHandler;
  368. self.destructiveHandler = destructiveHandler;
  369. }
  370. return self;
  371. }
  372. - (nonnull instancetype)initWithProgressViewAndTitle:(nullable NSString *)title
  373. message:(nullable NSString *)message
  374. style:(LGAlertViewStyle)style
  375. progress:(float)progress
  376. progressLabelText:(NSString *)progressLabelText
  377. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  378. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  379. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  380. actionHandler:(LGAlertViewActionHandler)actionHandler
  381. cancelHandler:(LGAlertViewHandler)cancelHandler
  382. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  383. self = [self initWithProgressViewAndTitle:title
  384. message:message
  385. style:style
  386. progress:progress
  387. progressLabelText:progressLabelText
  388. buttonTitles:buttonTitles
  389. cancelButtonTitle:cancelButtonTitle
  390. destructiveButtonTitle:destructiveButtonTitle];
  391. if (self) {
  392. self.actionHandler = actionHandler;
  393. self.cancelHandler = cancelHandler;
  394. self.destructiveHandler = destructiveHandler;
  395. }
  396. return self;
  397. }
  398. - (nonnull instancetype)initWithTextFieldsAndTitle:(nullable NSString *)title
  399. message:(nullable NSString *)message
  400. numberOfTextFields:(NSUInteger)numberOfTextFields
  401. textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
  402. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  403. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  404. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  405. actionHandler:(LGAlertViewActionHandler)actionHandler
  406. cancelHandler:(LGAlertViewHandler)cancelHandler
  407. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  408. self = [self initWithTextFieldsAndTitle:title
  409. message:message
  410. numberOfTextFields:numberOfTextFields
  411. textFieldsSetupHandler:textFieldsSetupHandler
  412. buttonTitles:buttonTitles
  413. cancelButtonTitle:cancelButtonTitle
  414. destructiveButtonTitle:destructiveButtonTitle];
  415. if (self) {
  416. self.actionHandler = actionHandler;
  417. self.cancelHandler = cancelHandler;
  418. self.destructiveHandler = destructiveHandler;
  419. }
  420. return self;
  421. }
  422. + (nonnull instancetype)alertViewWithTitle:(nullable NSString *)title
  423. message:(nullable NSString *)message
  424. style:(LGAlertViewStyle)style
  425. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  426. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  427. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  428. actionHandler:(LGAlertViewActionHandler)actionHandler
  429. cancelHandler:(LGAlertViewHandler)cancelHandler
  430. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  431. return [[self alloc] initWithTitle:title
  432. message:message
  433. style:style
  434. buttonTitles:buttonTitles
  435. cancelButtonTitle:cancelButtonTitle
  436. destructiveButtonTitle:destructiveButtonTitle
  437. actionHandler:actionHandler
  438. cancelHandler:cancelHandler
  439. destructiveHandler:destructiveHandler];
  440. }
  441. + (nonnull instancetype)alertViewWithViewAndTitle:(nullable NSString *)title
  442. message:(nullable NSString *)message
  443. style:(LGAlertViewStyle)style
  444. view:(nullable UIView *)view
  445. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  446. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  447. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  448. actionHandler:(LGAlertViewActionHandler)actionHandler
  449. cancelHandler:(LGAlertViewHandler)cancelHandler
  450. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  451. return [[self alloc] initWithViewAndTitle:title
  452. message:message
  453. style:style
  454. view:view
  455. buttonTitles:buttonTitles
  456. cancelButtonTitle:cancelButtonTitle
  457. destructiveButtonTitle:destructiveButtonTitle
  458. actionHandler:actionHandler
  459. cancelHandler:cancelHandler
  460. destructiveHandler:destructiveHandler];
  461. }
  462. + (nonnull instancetype)alertViewWithActivityIndicatorAndTitle:(nullable NSString *)title
  463. message:(nullable NSString *)message
  464. style:(LGAlertViewStyle)style
  465. progressLabelText:(nullable NSString *)progressLabelText
  466. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  467. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  468. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  469. actionHandler:(LGAlertViewActionHandler)actionHandler
  470. cancelHandler:(LGAlertViewHandler)cancelHandler
  471. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  472. return [[self alloc] initWithActivityIndicatorAndTitle:title
  473. message:message
  474. style:style
  475. progressLabelText:progressLabelText
  476. buttonTitles:buttonTitles
  477. cancelButtonTitle:cancelButtonTitle
  478. destructiveButtonTitle:destructiveButtonTitle
  479. actionHandler:actionHandler
  480. cancelHandler:cancelHandler
  481. destructiveHandler:destructiveHandler];
  482. }
  483. + (nonnull instancetype)alertViewWithProgressViewAndTitle:(nullable NSString *)title
  484. message:(nullable NSString *)message
  485. style:(LGAlertViewStyle)style
  486. progress:(float)progress
  487. progressLabelText:(NSString *)progressLabelText
  488. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  489. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  490. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  491. actionHandler:(LGAlertViewActionHandler)actionHandler
  492. cancelHandler:(LGAlertViewHandler)cancelHandler
  493. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  494. return [[self alloc] initWithProgressViewAndTitle:title
  495. message:message
  496. style:style
  497. progress:progress
  498. progressLabelText:progressLabelText
  499. buttonTitles:buttonTitles
  500. cancelButtonTitle:cancelButtonTitle
  501. destructiveButtonTitle:destructiveButtonTitle
  502. actionHandler:actionHandler
  503. cancelHandler:cancelHandler
  504. destructiveHandler:destructiveHandler];
  505. }
  506. + (nonnull instancetype)alertViewWithTextFieldsAndTitle:(nullable NSString *)title
  507. message:(nullable NSString *)message
  508. numberOfTextFields:(NSUInteger)numberOfTextFields
  509. textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
  510. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  511. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  512. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  513. actionHandler:(LGAlertViewActionHandler)actionHandler
  514. cancelHandler:(LGAlertViewHandler)cancelHandler
  515. destructiveHandler:(LGAlertViewHandler)destructiveHandler {
  516. return [[self alloc] initWithTextFieldsAndTitle:title
  517. message:message
  518. numberOfTextFields:numberOfTextFields
  519. textFieldsSetupHandler:textFieldsSetupHandler
  520. buttonTitles:buttonTitles
  521. cancelButtonTitle:cancelButtonTitle
  522. destructiveButtonTitle:destructiveButtonTitle
  523. actionHandler:actionHandler
  524. cancelHandler:cancelHandler
  525. destructiveHandler:destructiveHandler];
  526. }
  527. #pragma mark -
  528. - (nonnull instancetype)initWithTitle:(nullable NSString *)title
  529. message:(nullable NSString *)message
  530. style:(LGAlertViewStyle)style
  531. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  532. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  533. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  534. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  535. self = [self initWithTitle:title
  536. message:message
  537. style:style
  538. buttonTitles:buttonTitles
  539. cancelButtonTitle:cancelButtonTitle
  540. destructiveButtonTitle:destructiveButtonTitle];
  541. if (self) {
  542. self.delegate = delegate;
  543. }
  544. return self;
  545. }
  546. - (nonnull instancetype)initWithViewAndTitle:(nullable NSString *)title
  547. message:(nullable NSString *)message
  548. style:(LGAlertViewStyle)style
  549. view:(nullable UIView *)view
  550. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  551. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  552. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  553. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  554. self = [self initWithViewAndTitle:title
  555. message:message
  556. style:style
  557. view:view
  558. buttonTitles:buttonTitles
  559. cancelButtonTitle:cancelButtonTitle
  560. destructiveButtonTitle:destructiveButtonTitle];
  561. if (self) {
  562. self.delegate = delegate;
  563. }
  564. return self;
  565. }
  566. - (nonnull instancetype)initWithActivityIndicatorAndTitle:(nullable NSString *)title
  567. message:(nullable NSString *)message
  568. style:(LGAlertViewStyle)style
  569. progressLabelText:(nullable NSString *)progressLabelText
  570. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  571. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  572. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  573. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  574. self = [self initWithActivityIndicatorAndTitle:title
  575. message:message
  576. style:style
  577. progressLabelText:progressLabelText
  578. buttonTitles:buttonTitles
  579. cancelButtonTitle:cancelButtonTitle
  580. destructiveButtonTitle:destructiveButtonTitle];
  581. if (self) {
  582. self.delegate = delegate;
  583. }
  584. return self;
  585. }
  586. - (nonnull instancetype)initWithProgressViewAndTitle:(nullable NSString *)title
  587. message:(nullable NSString *)message
  588. style:(LGAlertViewStyle)style
  589. progress:(float)progress
  590. progressLabelText:(NSString *)progressLabelText
  591. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  592. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  593. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  594. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  595. self = [self initWithProgressViewAndTitle:title
  596. message:message
  597. style:style
  598. progress:progress
  599. progressLabelText:progressLabelText
  600. buttonTitles:buttonTitles
  601. cancelButtonTitle:cancelButtonTitle
  602. destructiveButtonTitle:destructiveButtonTitle];
  603. if (self) {
  604. self.delegate = delegate;
  605. }
  606. return self;
  607. }
  608. - (nonnull instancetype)initWithTextFieldsAndTitle:(nullable NSString *)title
  609. message:(nullable NSString *)message
  610. numberOfTextFields:(NSUInteger)numberOfTextFields
  611. textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
  612. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  613. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  614. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  615. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  616. self = [self initWithTextFieldsAndTitle:title
  617. message:message
  618. numberOfTextFields:numberOfTextFields
  619. textFieldsSetupHandler:textFieldsSetupHandler
  620. buttonTitles:buttonTitles
  621. cancelButtonTitle:cancelButtonTitle
  622. destructiveButtonTitle:destructiveButtonTitle];
  623. if (self) {
  624. self.delegate = delegate;
  625. }
  626. return self;
  627. }
  628. + (nonnull instancetype)alertViewWithTitle:(nullable NSString *)title
  629. message:(nullable NSString *)message
  630. style:(LGAlertViewStyle)style
  631. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  632. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  633. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  634. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  635. return [[self alloc] initWithTitle:title
  636. message:message
  637. style:style
  638. buttonTitles:buttonTitles
  639. cancelButtonTitle:cancelButtonTitle
  640. destructiveButtonTitle:destructiveButtonTitle
  641. delegate:delegate];
  642. }
  643. + (nonnull instancetype)alertViewWithViewAndTitle:(nullable NSString *)title
  644. message:(nullable NSString *)message
  645. style:(LGAlertViewStyle)style
  646. view:(nullable UIView *)view
  647. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  648. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  649. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  650. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  651. return [[self alloc] initWithViewAndTitle:title
  652. message:message
  653. style:style
  654. view:view
  655. buttonTitles:buttonTitles
  656. cancelButtonTitle:cancelButtonTitle
  657. destructiveButtonTitle:destructiveButtonTitle
  658. delegate:delegate];
  659. }
  660. + (nonnull instancetype)alertViewWithActivityIndicatorAndTitle:(nullable NSString *)title
  661. message:(nullable NSString *)message
  662. style:(LGAlertViewStyle)style
  663. progressLabelText:(nullable NSString *)progressLabelText
  664. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  665. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  666. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  667. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  668. return [[self alloc] initWithActivityIndicatorAndTitle:title
  669. message:message
  670. style:style
  671. progressLabelText:progressLabelText
  672. buttonTitles:buttonTitles
  673. cancelButtonTitle:cancelButtonTitle
  674. destructiveButtonTitle:destructiveButtonTitle
  675. delegate:delegate];
  676. }
  677. + (nonnull instancetype)alertViewWithProgressViewAndTitle:(nullable NSString *)title
  678. message:(nullable NSString *)message
  679. style:(LGAlertViewStyle)style
  680. progress:(float)progress
  681. progressLabelText:(NSString *)progressLabelText
  682. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  683. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  684. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  685. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  686. return [[self alloc] initWithProgressViewAndTitle:title
  687. message:message
  688. style:style
  689. progress:progress
  690. progressLabelText:progressLabelText
  691. buttonTitles:buttonTitles
  692. cancelButtonTitle:cancelButtonTitle
  693. destructiveButtonTitle:destructiveButtonTitle
  694. delegate:delegate];
  695. }
  696. + (nonnull instancetype)alertViewWithTextFieldsAndTitle:(nullable NSString *)title
  697. message:(nullable NSString *)message
  698. numberOfTextFields:(NSUInteger)numberOfTextFields
  699. textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
  700. buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
  701. cancelButtonTitle:(nullable NSString *)cancelButtonTitle
  702. destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle
  703. delegate:(nullable id<LGAlertViewDelegate>)delegate {
  704. return [[self alloc] alertViewWithTextFieldsAndTitle:title
  705. message:message
  706. numberOfTextFields:numberOfTextFields
  707. textFieldsSetupHandler:textFieldsSetupHandler
  708. buttonTitles:buttonTitles
  709. cancelButtonTitle:cancelButtonTitle
  710. destructiveButtonTitle:destructiveButtonTitle
  711. delegate:delegate];
  712. }
  713. #pragma mark -
  714. - (nonnull instancetype)initAsAppearance {
  715. self = [super init];
  716. if (self) {
  717. _heightCell = [[LGAlertViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  718. // -----
  719. _windowLevel = LGAlertViewWindowLevelAboveStatusBar;
  720. _dismissOnAction = YES;
  721. _tintColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];
  722. _coverColor = [UIColor colorWithWhite:0.0 alpha:0.35];
  723. _coverBlurEffect = nil;
  724. _coverAlpha = 1.0;
  725. _backgroundColor = UIColor.whiteColor;
  726. _backgroundBlurEffect = nil;
  727. _textFieldsHeight = 44.0;
  728. _offsetVertical = 8.0;
  729. _cancelButtonOffsetY = 8.0;
  730. _heightMax = NSNotFound;
  731. _width = NSNotFound;
  732. _separatorsColor = [UIColor colorWithWhite:0.85 alpha:1.0];
  733. _indicatorStyle = UIScrollViewIndicatorStyleBlack;
  734. _showsVerticalScrollIndicator = NO;
  735. _padShowsActionSheetFromBottom = NO;
  736. _oneRowOneButton = NO;
  737. _shouldDismissAnimated = YES;
  738. _layerCornerRadius = LGAlertViewHelper.systemVersion < 9.0 ? 6.0 : 12.0;
  739. _layerBorderColor = nil;
  740. _layerBorderWidth = 0.0;
  741. _layerShadowColor = nil;
  742. _layerShadowRadius = 0.0;
  743. _layerShadowOffset = CGPointZero;
  744. _animationDuration = 0.5;
  745. _initialScale = 1.2;
  746. _finalScale = 0.95;
  747. _titleTextColor = nil;
  748. _titleTextAlignment = NSTextAlignmentCenter;
  749. _titleFont = nil;
  750. _messageTextColor = nil;
  751. _messageTextAlignment = NSTextAlignmentCenter;
  752. _messageFont = [UIFont systemFontOfSize:14.0];
  753. _buttonsTitleColor = self.tintColor;
  754. _buttonsTitleColorHighlighted = UIColor.whiteColor;
  755. _buttonsTitleColorDisabled = UIColor.grayColor;
  756. _buttonsTextAlignment = NSTextAlignmentCenter;
  757. _buttonsFont = [UIFont systemFontOfSize:18.0];
  758. _buttonsBackgroundColor = UIColor.clearColor;
  759. _buttonsBackgroundColorHighlighted = self.tintColor;
  760. _buttonsBackgroundColorDisabled = nil;
  761. _buttonsNumberOfLines = 1;
  762. _buttonsLineBreakMode = NSLineBreakByTruncatingMiddle;
  763. _buttonsMinimumScaleFactor = 14.0 / 18.0;
  764. _buttonsAdjustsFontSizeToFitWidth = YES;
  765. _buttonsEnabled = YES;
  766. _buttonsIconPosition = LGAlertViewButtonIconPositionLeft;
  767. _cancelButtonTitleColor = self.tintColor;
  768. _cancelButtonTitleColorHighlighted = UIColor.whiteColor;
  769. _cancelButtonTitleColorDisabled = UIColor.grayColor;
  770. _cancelButtonTextAlignment = NSTextAlignmentCenter;
  771. _cancelButtonFont = [UIFont boldSystemFontOfSize:18.0];
  772. _cancelButtonBackgroundColor = UIColor.clearColor;
  773. _cancelButtonBackgroundColorHighlighted = self.tintColor;
  774. _cancelButtonBackgroundColorDisabled = nil;
  775. _cancelButtonNumberOfLines = 1;
  776. _cancelButtonLineBreakMode = NSLineBreakByTruncatingMiddle;
  777. _cancelButtonMinimumScaleFactor = 14.0 / 18.0;
  778. _cancelButtonAdjustsFontSizeToFitWidth = YES;
  779. _cancelButtonEnabled = YES;
  780. _cancelButtonIconPosition = LGAlertViewButtonIconPositionLeft;
  781. _destructiveButtonTitleColor = UIColor.redColor;
  782. _destructiveButtonTitleColorHighlighted = UIColor.whiteColor;
  783. _destructiveButtonTitleColorDisabled = UIColor.grayColor;
  784. _destructiveButtonTextAlignment = NSTextAlignmentCenter;
  785. _destructiveButtonFont = [UIFont systemFontOfSize:18.0];
  786. _destructiveButtonBackgroundColor = UIColor.clearColor;
  787. _destructiveButtonBackgroundColorHighlighted = UIColor.redColor;
  788. _destructiveButtonBackgroundColorDisabled = nil;
  789. _destructiveButtonNumberOfLines = 1;
  790. _destructiveButtonLineBreakMode = NSLineBreakByTruncatingMiddle;
  791. _destructiveButtonMinimumScaleFactor = 14.0 / 18.0;
  792. _destructiveButtonAdjustsFontSizeToFitWidth = YES;
  793. _destructiveButtonEnabled = YES;
  794. _destructiveButtonIconPosition = LGAlertViewButtonIconPositionLeft;
  795. _activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  796. _activityIndicatorViewColor = self.tintColor;
  797. _progressViewProgressTintColor = self.tintColor;
  798. _progressViewTrackTintColor = [UIColor colorWithWhite:0.8 alpha:1.0];
  799. _progressViewProgressImage = nil;
  800. _progressViewTrackImage = nil;
  801. _progressLabelTextColor = UIColor.blackColor;
  802. _progressLabelTextAlignment = NSTextAlignmentCenter;
  803. _progressLabelFont = [UIFont systemFontOfSize:14.0];
  804. _progressLabelNumberOfLines = 1;
  805. _progressLabelLineBreakMode = NSLineBreakByTruncatingTail;
  806. _textFieldsBackgroundColor = [UIColor colorWithWhite:0.97 alpha:1.0];
  807. _textFieldsTextColor = UIColor.blackColor;
  808. _textFieldsFont = [UIFont systemFontOfSize:16.0];
  809. _textFieldsTextAlignment = NSTextAlignmentLeft;
  810. _textFieldsClearsOnBeginEditing = NO;
  811. _textFieldsAdjustsFontSizeToFitWidth = NO;
  812. _textFieldsMinimumFontSize = 12.0;
  813. _textFieldsClearButtonMode = UITextFieldViewModeAlways;
  814. }
  815. return self;
  816. }
  817. #pragma mark - Defaults
  818. - (void)setupDefaults {
  819. self.buttonsEnabledArray = [NSMutableArray new];
  820. for (NSUInteger i = 0; i < self.buttonTitles.count; i++) {
  821. [self.buttonsEnabledArray addObject:@YES];
  822. }
  823. LGAlertView *appearance = [LGAlertView appearance];
  824. // -----
  825. _heightCell = appearance.heightCell;
  826. // -----
  827. _windowLevel = appearance.windowLevel;
  828. if (appearance.isUserCancelOnTouch) {
  829. _cancelOnTouch = appearance.cancelOnTouch;
  830. }
  831. else {
  832. _cancelOnTouch = self.type != LGAlertViewTypeActivityIndicator && self.type != LGAlertViewTypeProgressView && self.type != LGAlertViewTypeTextFields;
  833. }
  834. _dismissOnAction = appearance.dismissOnAction;
  835. _tag = NSNotFound;
  836. _tintColor = appearance.tintColor;
  837. _coverColor = appearance.coverColor;
  838. _coverBlurEffect = appearance.coverBlurEffect;
  839. _coverAlpha = appearance.coverAlpha;
  840. _backgroundColor = appearance.backgroundColor;
  841. _backgroundBlurEffect = appearance.backgroundBlurEffect;
  842. if (appearance.isUserButtonsHeight) {
  843. _buttonsHeight = appearance.buttonsHeight;
  844. }
  845. else {
  846. _buttonsHeight = (self.style == LGAlertViewStyleAlert || LGAlertViewHelper.systemVersion < 9.0) ? 44.0 : 56.0;
  847. }
  848. _textFieldsHeight = appearance.textFieldsHeight;
  849. _offsetVertical = appearance.offsetVertical;
  850. _cancelButtonOffsetY = appearance.cancelButtonOffsetY;
  851. _heightMax = appearance.heightMax;
  852. _width = appearance.width;
  853. _separatorsColor = appearance.separatorsColor;
  854. _indicatorStyle = appearance.indicatorStyle;
  855. _showsVerticalScrollIndicator = appearance.showsVerticalScrollIndicator;
  856. _padShowsActionSheetFromBottom = appearance.padShowsActionSheetFromBottom;
  857. _oneRowOneButton = appearance.oneRowOneButton;
  858. _shouldDismissAnimated = appearance.shouldDismissAnimated;
  859. _layerCornerRadius = appearance.layerCornerRadius;
  860. _layerBorderColor = appearance.layerBorderColor;
  861. _layerBorderWidth = appearance.layerBorderWidth;
  862. _layerShadowColor = appearance.layerShadowColor;
  863. _layerShadowRadius = appearance.layerShadowRadius;
  864. _layerShadowOffset = appearance.layerShadowOffset;
  865. _animationDuration = appearance.animationDuration;
  866. _initialScale = appearance.initialScale;
  867. _finalScale = appearance.finalScale;
  868. if (appearance.isUserTitleTextColor) {
  869. _titleTextColor = appearance.titleTextColor;
  870. }
  871. else {
  872. _titleTextColor = self.style == LGAlertViewStyleAlert ? UIColor.blackColor : UIColor.grayColor;
  873. }
  874. _titleTextAlignment = appearance.titleTextAlignment;
  875. if (appearance.isUserTitleFont) {
  876. _titleFont = appearance.titleFont;
  877. }
  878. else {
  879. _titleFont = [UIFont boldSystemFontOfSize:self.style == LGAlertViewStyleAlert ? 18.0 : 14.0];
  880. }
  881. if (appearance.isUserMessageTextColor) {
  882. _messageTextColor = appearance.messageTextColor;
  883. }
  884. else {
  885. _messageTextColor = self.style == LGAlertViewStyleAlert ? UIColor.blackColor : UIColor.grayColor;
  886. }
  887. _messageTextAlignment = appearance.messageTextAlignment;
  888. _messageFont = appearance.messageFont;
  889. _buttonsTitleColor = appearance.buttonsTitleColor;
  890. _buttonsTitleColorHighlighted = appearance.buttonsTitleColorHighlighted;
  891. _buttonsTitleColorDisabled = appearance.buttonsTitleColorDisabled;
  892. _buttonsTextAlignment = appearance.buttonsTextAlignment;
  893. _buttonsFont = appearance.buttonsFont;
  894. _buttonsBackgroundColor = appearance.buttonsBackgroundColor;
  895. _buttonsBackgroundColorHighlighted = appearance.buttonsBackgroundColorHighlighted;
  896. _buttonsBackgroundColorDisabled = appearance.buttonsBackgroundColorDisabled;
  897. _buttonsNumberOfLines = appearance.buttonsNumberOfLines;
  898. _buttonsLineBreakMode = appearance.buttonsLineBreakMode;
  899. _buttonsMinimumScaleFactor = appearance.buttonsMinimumScaleFactor;
  900. _buttonsAdjustsFontSizeToFitWidth = appearance.buttonsAdjustsFontSizeToFitWidth;
  901. _buttonsEnabled = appearance.buttonsEnabled;
  902. _buttonsIconPosition = appearance.buttonsIconPosition;
  903. _cancelButtonTitleColor = appearance.cancelButtonTitleColor;
  904. _cancelButtonTitleColorHighlighted = appearance.cancelButtonTitleColorHighlighted;
  905. _cancelButtonTitleColorDisabled = appearance.cancelButtonTitleColorDisabled;
  906. _cancelButtonTextAlignment = appearance.cancelButtonTextAlignment;
  907. _cancelButtonFont = appearance.cancelButtonFont;
  908. _cancelButtonBackgroundColor = appearance.cancelButtonBackgroundColor;
  909. _cancelButtonBackgroundColorHighlighted = appearance.cancelButtonBackgroundColorHighlighted;
  910. _cancelButtonBackgroundColorDisabled = appearance.cancelButtonBackgroundColorDisabled;
  911. _cancelButtonNumberOfLines = appearance.cancelButtonNumberOfLines;
  912. _cancelButtonLineBreakMode = appearance.cancelButtonLineBreakMode;
  913. _cancelButtonMinimumScaleFactor = appearance.cancelButtonMinimumScaleFactor;
  914. _cancelButtonAdjustsFontSizeToFitWidth = appearance.cancelButtonAdjustsFontSizeToFitWidth;
  915. _cancelButtonEnabled = appearance.cancelButtonEnabled;
  916. _cancelButtonIconPosition = appearance.cancelButtonIconPosition;
  917. _destructiveButtonTitleColor = appearance.destructiveButtonTitleColor;
  918. _destructiveButtonTitleColorHighlighted = appearance.destructiveButtonTitleColorHighlighted;
  919. _destructiveButtonTitleColorDisabled = appearance.destructiveButtonTitleColorDisabled;
  920. _destructiveButtonTextAlignment = appearance.destructiveButtonTextAlignment;
  921. _destructiveButtonFont = appearance.destructiveButtonFont;
  922. _destructiveButtonBackgroundColor = appearance.destructiveButtonBackgroundColor;
  923. _destructiveButtonBackgroundColorHighlighted = appearance.destructiveButtonBackgroundColorHighlighted;
  924. _destructiveButtonBackgroundColorDisabled = appearance.destructiveButtonBackgroundColorDisabled;
  925. _destructiveButtonNumberOfLines = appearance.destructiveButtonNumberOfLines;
  926. _destructiveButtonLineBreakMode = appearance.destructiveButtonLineBreakMode;
  927. _destructiveButtonMinimumScaleFactor = appearance.destructiveButtonMinimumScaleFactor;
  928. _destructiveButtonAdjustsFontSizeToFitWidth = appearance.destructiveButtonAdjustsFontSizeToFitWidth;
  929. _destructiveButtonEnabled = appearance.destructiveButtonEnabled;
  930. _destructiveButtonIconPosition = appearance.destructiveButtonIconPosition;
  931. _activityIndicatorViewStyle = appearance.activityIndicatorViewStyle;
  932. _activityIndicatorViewColor = appearance.activityIndicatorViewColor;
  933. _progressViewProgressTintColor = appearance.progressViewProgressTintColor;
  934. _progressViewTrackTintColor = appearance.progressViewTrackTintColor;
  935. _progressViewProgressImage = appearance.progressViewProgressImage;
  936. _progressViewTrackImage = appearance.progressViewTrackImage;
  937. _progressLabelTextColor = appearance.progressLabelTextColor;
  938. _progressLabelTextAlignment = appearance.progressLabelTextAlignment;
  939. _progressLabelFont = appearance.progressLabelFont;
  940. _progressLabelNumberOfLines = appearance.progressLabelNumberOfLines;
  941. _progressLabelLineBreakMode = appearance.progressLabelLineBreakMode;
  942. _textFieldsBackgroundColor = appearance.textFieldsBackgroundColor;
  943. _textFieldsTextColor = appearance.textFieldsTextColor;
  944. _textFieldsFont = appearance.textFieldsFont;
  945. _textFieldsTextAlignment = appearance.textFieldsTextAlignment;
  946. _textFieldsClearsOnBeginEditing = appearance.textFieldsClearsOnBeginEditing;
  947. _textFieldsAdjustsFontSizeToFitWidth = appearance.textFieldsAdjustsFontSizeToFitWidth;
  948. _textFieldsMinimumFontSize = appearance.textFieldsMinimumFontSize;
  949. _textFieldsClearButtonMode = appearance.textFieldsClearButtonMode;
  950. // -----
  951. self.view = [UIView new];
  952. self.view.backgroundColor = UIColor.clearColor;
  953. self.view.userInteractionEnabled = YES;
  954. self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  955. self.backgroundView = [[UIVisualEffectView alloc] initWithEffect:self.coverBlurEffect];
  956. self.backgroundView.alpha = 0.0;
  957. self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  958. [self.view addSubview:self.backgroundView];
  959. // -----
  960. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAction:)];
  961. tapGesture.delegate = self;
  962. [self.backgroundView addGestureRecognizer:tapGesture];
  963. // -----
  964. self.viewController = [[LGAlertViewController alloc] initWithAlertView:self view:self.view];
  965. self.window = [[LGAlertViewWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  966. self.window.hidden = YES;
  967. self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  968. self.window.opaque = NO;
  969. self.window.backgroundColor = UIColor.clearColor;
  970. self.window.rootViewController = self.viewController;
  971. // -----
  972. self.initialized = YES;
  973. }
  974. #pragma mark - Class load
  975. + (void)load {
  976. static dispatch_once_t token;
  977. dispatch_once(&token, ^{
  978. [[LGAlertViewWindowsObserver sharedInstance] startObserving];
  979. });
  980. }
  981. #pragma mark - Dealloc
  982. - (void)dealloc {
  983. [self removeObservers];
  984. #if DEBUG && LG_ALERT_VIEW_DEBUG
  985. NSLog(@"LGAlertView DEALLOCATED");
  986. #endif
  987. }
  988. #pragma mark - UIAppearance
  989. + (instancetype)appearance {
  990. return [self sharedAlertViewForAppearance];
  991. }
  992. + (instancetype)appearanceWhenContainedIn:(Class<UIAppearanceContainer>)ContainerClass, ... {
  993. return [self sharedAlertViewForAppearance];
  994. }
  995. + (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait {
  996. return [self sharedAlertViewForAppearance];
  997. }
  998. + (instancetype)appearanceForTraitCollection:(UITraitCollection *)trait whenContainedIn:(Class<UIAppearanceContainer>)ContainerClass, ... {
  999. return [self sharedAlertViewForAppearance];
  1000. }
  1001. + (instancetype)sharedAlertViewForAppearance {
  1002. static LGAlertView *alertView;
  1003. static dispatch_once_t onceToken;
  1004. dispatch_once(&onceToken, ^{
  1005. alertView = [[LGAlertView alloc] initAsAppearance];
  1006. });
  1007. return alertView;
  1008. }
  1009. #pragma mark - Observers
  1010. - (void)addObservers {
  1011. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardVisibleChanged:) name:UIKeyboardWillShowNotification object:nil];
  1012. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardVisibleChanged:) name:UIKeyboardWillHideNotification object:nil];
  1013. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
  1014. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowVisibleChanged:) name:UIWindowDidBecomeVisibleNotification object:nil];
  1015. }
  1016. - (void)removeObservers {
  1017. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  1018. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  1019. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
  1020. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
  1021. }
  1022. #pragma mark - Window notifications
  1023. - (void)windowVisibleChanged:(NSNotification *)notification {
  1024. if (notification.object == self.window) {
  1025. [self.viewController.view setNeedsLayout];
  1026. }
  1027. }
  1028. #pragma mark - Keyboard notifications
  1029. - (void)keyboardVisibleChanged:(NSNotification *)notification {
  1030. if (!self.isShowing || self.window.isHidden || !self.window.isKeyWindow) return;
  1031. [LGAlertViewHelper
  1032. keyboardAnimateWithNotificationUserInfo:notification.userInfo
  1033. animations:^(CGFloat keyboardHeight) {
  1034. if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
  1035. self.keyboardHeight = keyboardHeight;
  1036. }
  1037. else {
  1038. self.keyboardHeight = 0.0;
  1039. }
  1040. [self layoutValidateWithSize:self.view.bounds.size];
  1041. }];
  1042. }
  1043. - (void)keyboardFrameChanged:(NSNotification *)notification {
  1044. if (!self.isShowing ||
  1045. self.window.isHidden ||
  1046. !self.window.isKeyWindow ||
  1047. [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] != 0.0) {
  1048. return;
  1049. }
  1050. self.keyboardHeight = CGRectGetHeight([notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]);
  1051. }
  1052. #pragma mark - UITextField Delegate
  1053. - (BOOL)textFieldShouldReturn:(LGAlertViewTextField *)textField {
  1054. if (textField.returnKeyType == UIReturnKeyNext) {
  1055. if (self.textFieldsArray.count > textField.tag + 1) {
  1056. LGAlertViewTextField *nextTextField = self.textFieldsArray[textField.tag + 1];
  1057. [nextTextField becomeFirstResponder];
  1058. }
  1059. }
  1060. else if (textField.returnKeyType == UIReturnKeyDone) {
  1061. [textField resignFirstResponder];
  1062. }
  1063. return YES;
  1064. }
  1065. #pragma mark - UIGestureRecognizer Delegate
  1066. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  1067. return self.isCancelOnTouch;
  1068. }
  1069. #pragma mark - Setters and Getters
  1070. - (void)setTintColor:(UIColor *)tintColor {
  1071. _tintColor = tintColor;
  1072. if (!self.isUserButtonsTitleColor) {
  1073. self.buttonsTitleColor = tintColor;
  1074. }
  1075. if (!self.isUserCancelButtonTitleColor) {
  1076. self.cancelButtonTitleColor = tintColor;
  1077. }
  1078. if (!self.isUserButtonsBackgroundColorHighlighted) {
  1079. self.buttonsBackgroundColorHighlighted = tintColor;
  1080. }
  1081. if (!self.isUserCancelButtonBackgroundColorHighlighted) {
  1082. self.cancelButtonBackgroundColorHighlighted = tintColor;
  1083. }
  1084. if (!self.isUserActivityIndicatorViewColor) {
  1085. self.activityIndicatorViewColor = tintColor;
  1086. }
  1087. if (!self.isUserProgressViewProgressTintColor) {
  1088. self.progressViewProgressTintColor = tintColor;
  1089. }
  1090. }
  1091. - (void)setCancelOnTouch:(BOOL)cancelOnTouch {
  1092. _cancelOnTouch = cancelOnTouch;
  1093. self.userCancelOnTouch = YES;
  1094. }
  1095. - (void)setButtonsHeight:(CGFloat)buttonsHeight {
  1096. _buttonsHeight = buttonsHeight;
  1097. self.userButtonsHeight = YES;
  1098. }
  1099. - (void)setTitleTextColor:(UIColor *)titleTextColor {
  1100. _titleTextColor = titleTextColor;
  1101. self.userTitleTextColor = YES;
  1102. }
  1103. - (void)setTitleFont:(UIFont *)titleFont {
  1104. _titleFont = titleFont;
  1105. self.userTitleFont = YES;
  1106. }
  1107. - (void)setMessageTextColor:(UIColor *)messageTextColor {
  1108. _messageTextColor = messageTextColor;
  1109. self.userMessageTextColor = YES;
  1110. }
  1111. - (void)setButtonsTitleColor:(UIColor *)buttonsTitleColor {
  1112. _buttonsTitleColor = buttonsTitleColor;
  1113. self.userButtonsTitleColor = YES;
  1114. }
  1115. - (void)setButtonsBackgroundColorHighlighted:(UIColor *)buttonsBackgroundColorHighlighted {
  1116. _buttonsBackgroundColorHighlighted = buttonsBackgroundColorHighlighted;
  1117. self.userButtonsBackgroundColorHighlighted = YES;
  1118. }
  1119. - (void)setCancelButtonTitleColor:(UIColor *)cancelButtonTitleColor {
  1120. _cancelButtonTitleColor = cancelButtonTitleColor;
  1121. self.userCancelButtonTitleColor = YES;
  1122. }
  1123. - (void)setCancelButtonBackgroundColorHighlighted:(UIColor *)cancelButtonBackgroundColorHighlighted {
  1124. _cancelButtonBackgroundColorHighlighted = cancelButtonBackgroundColorHighlighted;
  1125. self.userCancelButtonBackgroundColorHighlighted = YES;
  1126. }
  1127. - (void)setActivityIndicatorViewColor:(UIColor *)activityIndicatorViewColor {
  1128. _activityIndicatorViewColor = activityIndicatorViewColor;
  1129. self.userActivityIndicatorViewColor = YES;
  1130. }
  1131. - (void)setProgressViewProgressTintColor:(UIColor *)progressViewProgressTintColor {
  1132. _progressViewProgressTintColor = progressViewProgressTintColor;
  1133. self.userProgressViewProgressTintColor = YES;
  1134. }
  1135. #pragma mark -
  1136. - (void)setProgress:(float)progress {
  1137. if (self.type != LGAlertViewTypeProgressView) return;
  1138. _progress = progress;
  1139. [self.progressView setProgress:_progress animated:YES];
  1140. }
  1141. - (void)setProgressLabelText:(nullable NSString *)progressLabelText {
  1142. if (self.type != LGAlertViewTypeProgressView && self.type != LGAlertViewTypeActivityIndicator) return;
  1143. _progressLabelText = progressLabelText;
  1144. self.progressLabel.text = _progressLabelText;
  1145. }
  1146. - (void)setCancelButtonEnabled:(BOOL)cancelButtonEnabled {
  1147. _cancelButtonEnabled = cancelButtonEnabled;
  1148. if (self.cancelButtonTitle) {
  1149. if (self.tableView) {
  1150. LGAlertViewCell *cell = (LGAlertViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(self.buttonTitles.count - 1) inSection:0]];
  1151. cell.enabled = cancelButtonEnabled;
  1152. }
  1153. else if (self.scrollView) {
  1154. self.cancelButton.enabled = cancelButtonEnabled;
  1155. }
  1156. }
  1157. }
  1158. - (void)setDestructiveButtonEnabled:(BOOL)destructiveButtonEnabled {
  1159. _destructiveButtonEnabled = destructiveButtonEnabled;
  1160. if (self.destructiveButtonTitle) {
  1161. if (self.tableView) {
  1162. LGAlertViewCell *cell = (LGAlertViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
  1163. cell.enabled = destructiveButtonEnabled;
  1164. }
  1165. else if (self.scrollView) {
  1166. self.destructiveButton.enabled = destructiveButtonEnabled;
  1167. }
  1168. }
  1169. }
  1170. - (CGFloat)width {
  1171. CGSize size = LGAlertViewHelper.appWindow.bounds.size;
  1172. if (_width != NSNotFound) {
  1173. CGFloat result = MIN(size.width, size.height);
  1174. if (_width < result) {
  1175. result = _width;
  1176. }
  1177. return result;
  1178. }
  1179. // If we try to get width of appearance object
  1180. if (!self.isInitialized) return NSNotFound;
  1181. if (self.style == LGAlertViewStyleAlert || [LGAlertViewHelper isPadAndNotForce:self]) {
  1182. return 280.0; // 320.0 - (20.0 * 2.0)
  1183. }
  1184. if (LGAlertViewHelper.isPad) {
  1185. return 388.0; // 320.0 - (16.0 * 2.0)
  1186. }
  1187. return MIN(size.width, size.height) - 16.0; // MIN(size.width, size.height) - (8.0 * 2.0)
  1188. }
  1189. - (void)setDelegate:(id<LGAlertViewDelegate>)delegate {
  1190. _delegate = delegate;
  1191. if (!delegate) return;
  1192. if ([delegate respondsToSelector:@selector(alertView:buttonPressedWithTitle:index:)]) {
  1193. NSLog(@"WARNING: delegate method \"alertView:buttonPressedWithTitle:index:\" is DEPRECATED, use \"alertView:clickedButtonAtIndex:title:\" instead");
  1194. }
  1195. if ([delegate respondsToSelector:@selector(alertViewDestructiveButtonPressed:)]) {
  1196. NSLog(@"WARNING: delegate method \"alertViewDestructiveButtonPressed:\" is DEPRECATED, use \"alertViewDestructed:\" instead");
  1197. }
  1198. }
  1199. #pragma mark -
  1200. - (void)setProgress:(float)progress progressLabelText:(nullable NSString *)progressLabelText {
  1201. if (self.type != LGAlertViewTypeProgressView) return;
  1202. self.progress = progress;
  1203. self.progressLabelText = progressLabelText;
  1204. }
  1205. - (void)setButtonEnabled:(BOOL)enabled atIndex:(NSUInteger)index {
  1206. if (self.buttonTitles.count <= index) return;
  1207. self.buttonsEnabledArray[index] = @(enabled);
  1208. if (self.tableView) {
  1209. if (self.destructiveButtonTitle) {
  1210. index++;
  1211. }
  1212. LGAlertViewCell *cell = (LGAlertViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
  1213. cell.enabled = enabled;
  1214. }
  1215. else if (self.scrollView) {
  1216. switch (index) {
  1217. case 0:
  1218. self.firstButton.enabled = enabled;
  1219. break;
  1220. case 1:
  1221. self.secondButton.enabled = enabled;
  1222. break;
  1223. case 2:
  1224. self.thirdButton.enabled = enabled;
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. }
  1230. }
  1231. - (BOOL)isButtonEnabledAtIndex:(NSUInteger)index {
  1232. return [self.buttonsEnabledArray[index] boolValue];
  1233. }
  1234. - (void)setButtonPropertiesAtIndex:(NSUInteger)index
  1235. handler:(void(^ _Nonnull)(LGAlertViewButtonProperties * _Nonnull properties))handler {
  1236. if (!handler || self.buttonTitles.count <= index) return;
  1237. if (!self.buttonsPropertiesDictionary) {
  1238. self.buttonsPropertiesDictionary = [NSMutableDictionary new];
  1239. }
  1240. LGAlertViewButtonProperties *properties = self.buttonsPropertiesDictionary[@(index)];
  1241. if (!properties) {
  1242. properties = [LGAlertViewButtonProperties new];
  1243. }
  1244. handler(properties);
  1245. if (properties.isUserEnabled) {
  1246. self.buttonsEnabledArray[index] = @(properties.enabled);
  1247. }
  1248. [self.buttonsPropertiesDictionary setObject:properties forKey:@(index)];
  1249. }
  1250. - (void)forceCancel {
  1251. NSAssert(self.cancelButtonTitle, @"Cancel button is not exists");
  1252. [self cancelAction:nil];
  1253. }
  1254. - (void)forceDestructive {
  1255. NSAssert(self.destructiveButtonTitle, @"Destructive button is not exists");
  1256. [self destructiveAction:nil];
  1257. }
  1258. - (void)forceActionAtIndex:(NSUInteger)index {
  1259. NSAssert(self.buttonTitles.count > index, @"Button at index %lu is not exists", (long unsigned)index);
  1260. [self actionActionAtIndex:index title:self.buttonTitles[index + (self.tableView && self.destructiveButtonTitle ? 1 : 0)]];
  1261. }
  1262. #pragma mark - Table View Data Source
  1263. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  1264. return 1;
  1265. }
  1266. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  1267. return self.buttonTitles.count;
  1268. }
  1269. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  1270. LGAlertViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  1271. [self configCell:cell forRowAtIndexPath:indexPath];
  1272. return cell;
  1273. }
  1274. - (void)configCell:(LGAlertViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  1275. cell.textLabel.text = self.buttonTitles[indexPath.row];
  1276. if (self.destructiveButtonTitle && indexPath.row == 0) {
  1277. cell.titleColor = self.destructiveButtonTitleColor;
  1278. cell.titleColorHighlighted = self.destructiveButtonTitleColorHighlighted;
  1279. cell.titleColorDisabled = self.destructiveButtonTitleColorDisabled;
  1280. cell.backgroundColorNormal = self.destructiveButtonBackgroundColor;
  1281. cell.backgroundColorHighlighted = self.destructiveButtonBackgroundColorHighlighted;
  1282. cell.backgroundColorDisabled = self.destructiveButtonBackgroundColorDisabled;
  1283. cell.image = self.destructiveButtonIconImage;
  1284. cell.imageHighlighted = self.destructiveButtonIconImageHighlighted;
  1285. cell.imageDisabled = self.destructiveButtonIconImageDisabled;
  1286. cell.iconPosition = self.destructiveButtonIconPosition;
  1287. cell.separatorView.hidden = (indexPath.row == self.buttonTitles.count - 1);
  1288. cell.separatorView.backgroundColor = self.separatorsColor;
  1289. cell.textLabel.textAlignment = self.destructiveButtonTextAlignment;
  1290. cell.textLabel.font = self.destructiveButtonFont;
  1291. cell.textLabel.numberOfLines = self.destructiveButtonNumberOfLines;
  1292. cell.textLabel.lineBreakMode = self.destructiveButtonLineBreakMode;
  1293. cell.textLabel.adjustsFontSizeToFitWidth = self.destructiveButtonAdjustsFontSizeToFitWidth;
  1294. cell.textLabel.minimumScaleFactor = self.destructiveButtonMinimumScaleFactor;
  1295. cell.enabled = self.destructiveButtonEnabled;
  1296. }
  1297. else if (self.cancelButtonTitle && ![LGAlertViewHelper isCancelButtonSeparate:self] && indexPath.row == self.buttonTitles.count - 1) {
  1298. cell.titleColor = self.cancelButtonTitleColor;
  1299. cell.titleColorHighlighted = self.cancelButtonTitleColorHighlighted;
  1300. cell.titleColorDisabled = self.cancelButtonTitleColorDisabled;
  1301. cell.backgroundColorNormal = self.cancelButtonBackgroundColor;
  1302. cell.backgroundColorHighlighted = self.cancelButtonBackgroundColorHighlighted;
  1303. cell.backgroundColorDisabled = self.cancelButtonBackgroundColorDisabled;
  1304. cell.image = self.cancelButtonIconImage;
  1305. cell.imageHighlighted = self.cancelButtonIconImageHighlighted;
  1306. cell.imageDisabled = self.cancelButtonIconImageDisabled;
  1307. cell.iconPosition = self.cancelButtonIconPosition;
  1308. cell.separatorView.hidden = YES;
  1309. cell.separatorView.backgroundColor = self.separatorsColor;
  1310. cell.textLabel.textAlignment = self.cancelButtonTextAlignment;
  1311. cell.textLabel.font = self.cancelButtonFont;
  1312. cell.textLabel.numberOfLines = self.cancelButtonNumberOfLines;
  1313. cell.textLabel.lineBreakMode = self.cancelButtonLineBreakMode;
  1314. cell.textLabel.adjustsFontSizeToFitWidth = self.cancelButtonAdjustsFontSizeToFitWidth;
  1315. cell.textLabel.minimumScaleFactor = self.cancelButtonMinimumScaleFactor;
  1316. cell.enabled = self.cancelButtonEnabled;
  1317. }
  1318. else {
  1319. NSInteger buttonIndex = indexPath.row - (self.destructiveButtonTitle ? 1 : 0);
  1320. LGAlertViewButtonProperties *properties = nil;
  1321. if (self.buttonsPropertiesDictionary) {
  1322. properties = self.buttonsPropertiesDictionary[@(buttonIndex)];
  1323. }
  1324. cell.titleColor = (properties.isUserTitleColor ? properties.titleColor : self.buttonsTitleColor);
  1325. cell.titleColorHighlighted = (properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted);
  1326. cell.titleColorDisabled = (properties.isUserTitleColorDisabled ? properties.titleColorDisabled : self.buttonsTitleColorDisabled);
  1327. cell.backgroundColorNormal = (properties.isUserBackgroundColor ? properties.backgroundColor : self.buttonsBackgroundColor);
  1328. cell.backgroundColorHighlighted = (properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted);
  1329. cell.backgroundColorDisabled = (properties.isUserBackgroundColorDisabled ? properties.backgroundColorDisabled : self.buttonsBackgroundColorDisabled);
  1330. UIImage *image = nil;
  1331. if (properties.isUserIconImage) {
  1332. image = properties.iconImage;
  1333. }
  1334. else if (self.buttonsIconImages.count > buttonIndex) {
  1335. image = self.buttonsIconImages[buttonIndex];
  1336. }
  1337. cell.image = image;
  1338. UIImage *imageHighlighted = nil;
  1339. if (properties.isUserIconImageHighlighted) {
  1340. imageHighlighted = properties.iconImageHighlighted;
  1341. }
  1342. else if (self.buttonsIconImagesHighlighted.count > buttonIndex) {
  1343. imageHighlighted = self.buttonsIconImagesHighlighted[buttonIndex];
  1344. }
  1345. cell.imageHighlighted = imageHighlighted;
  1346. UIImage *imageDisabled = nil;
  1347. if (properties.isUserIconImageDisabled) {
  1348. imageDisabled = properties.iconImageDisabled;
  1349. }
  1350. else if (self.buttonsIconImagesDisabled.count > buttonIndex) {
  1351. imageDisabled = self.buttonsIconImagesDisabled[buttonIndex];
  1352. }
  1353. cell.imageDisabled = imageDisabled;
  1354. cell.iconPosition = (properties.isUserIconPosition ? properties.iconPosition : self.buttonsIconPosition);
  1355. cell.separatorView.hidden = (indexPath.row == self.buttonTitles.count - 1);
  1356. cell.separatorView.backgroundColor = self.separatorsColor;
  1357. cell.textLabel.textAlignment = (properties.isUserTextAlignment ? properties.textAlignment : self.buttonsTextAlignment);
  1358. cell.textLabel.font = (properties.isUserFont ? properties.font : self.buttonsFont);
  1359. cell.textLabel.numberOfLines = (properties.isUserNumberOfLines ? properties.numberOfLines : self.buttonsNumberOfLines);
  1360. cell.textLabel.lineBreakMode = (properties.isUserLineBreakMode ? properties.lineBreakMode : self.buttonsLineBreakMode);
  1361. cell.textLabel.adjustsFontSizeToFitWidth = (properties.isUserAdjustsFontSizeTofitWidth ? properties.adjustsFontSizeToFitWidth : self.buttonsAdjustsFontSizeToFitWidth);
  1362. cell.textLabel.minimumScaleFactor = (properties.isUserMinimimScaleFactor ? properties.minimumScaleFactor : self.buttonsMinimumScaleFactor);
  1363. cell.enabled = [self.buttonsEnabledArray[buttonIndex] boolValue];
  1364. }
  1365. }
  1366. #pragma mark - Table View Delegate
  1367. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  1368. [self configCell:self.heightCell forRowAtIndexPath:indexPath];
  1369. CGSize size = [self.heightCell sizeThatFits:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX)];
  1370. if (size.height < self.buttonsHeight) {
  1371. size.height = self.buttonsHeight;
  1372. }
  1373. return size.height;
  1374. }
  1375. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  1376. if (self.destructiveButtonTitle && indexPath.row == 0) {
  1377. [self destructiveAction:nil];
  1378. }
  1379. else if (self.cancelButtonTitle && indexPath.row == self.buttonTitles.count - 1 && ![LGAlertViewHelper isCancelButtonSeparate:self]) {
  1380. [self cancelAction:nil];
  1381. }
  1382. else {
  1383. NSUInteger index = indexPath.row;
  1384. if (self.destructiveButtonTitle) {
  1385. index--;
  1386. }
  1387. NSString *title = self.buttonTitles[indexPath.row];
  1388. // -----
  1389. [self actionActionAtIndex:index title:title];
  1390. }
  1391. }
  1392. #pragma mark - Show
  1393. - (void)showAnimated:(BOOL)animated completionHandler:(LGAlertViewCompletionHandler)completionHandler {
  1394. [self showAnimated:animated hidden:NO completionHandler:completionHandler];
  1395. }
  1396. - (void)showAnimated {
  1397. [self showAnimated:YES completionHandler:nil];
  1398. }
  1399. - (void)show {
  1400. [self showAnimated:NO completionHandler:nil];
  1401. }
  1402. - (void)showAnimated:(BOOL)animated hidden:(BOOL)hidden completionHandler:(LGAlertViewCompletionHandler)completionHandler {
  1403. if (!self.isValid || self.isShowing) return;
  1404. self.window.windowLevel = UIWindowLevelStatusBar + (self.windowLevel == LGAlertViewWindowLevelAboveStatusBar ? 1 : -1);
  1405. self.view.userInteractionEnabled = NO;
  1406. [self subviewsValidateWithSize:CGSizeZero];
  1407. [self layoutValidateWithSize:CGSizeZero];
  1408. self.showing = YES;
  1409. // -----
  1410. UIWindow *keyWindow = LGAlertViewHelper.keyWindow;
  1411. [keyWindow endEditing:YES];
  1412. if (!hidden && keyWindow != LGAlertViewHelper.appWindow) {
  1413. keyWindow.hidden = YES;
  1414. }
  1415. [self.window makeKeyAndVisible];
  1416. // -----
  1417. [self addObservers];
  1418. // -----
  1419. [self willShowCallback];
  1420. // -----
  1421. if (hidden) {
  1422. self.scrollView.hidden = YES;
  1423. self.backgroundView.hidden = YES;
  1424. self.shadowView.hidden = YES;
  1425. self.blurView.hidden = YES;
  1426. if ([LGAlertViewHelper isCancelButtonSeparate:self]) {
  1427. self.cancelButton.hidden = YES;
  1428. self.shadowCancelView.hidden = YES;
  1429. self.blurCancelView.hidden = YES;
  1430. }
  1431. }
  1432. // -----
  1433. if (animated) {
  1434. [LGAlertViewHelper
  1435. animateWithDuration:self.animationDuration
  1436. animations:^(void) {
  1437. [self showAnimations];
  1438. // -----
  1439. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewShowAnimationsNotification
  1440. object:self
  1441. userInfo:@{kLGAlertViewAnimationDuration: @(self.animationDuration)}];
  1442. if (self.showAnimationsBlock) {
  1443. self.showAnimationsBlock(self, self.animationDuration);
  1444. }
  1445. if (self.delegate && [self.delegate respondsToSelector:@selector(showAnimationsForAlertView:duration:)]) {
  1446. [self.delegate showAnimationsForAlertView:self duration:self.animationDuration];
  1447. }
  1448. }
  1449. completion:^(BOOL finished) {
  1450. if (!hidden) {
  1451. [self showComplete];
  1452. }
  1453. if (completionHandler) {
  1454. completionHandler();
  1455. }
  1456. }];
  1457. }
  1458. else {
  1459. [self showAnimations];
  1460. if (!hidden) {
  1461. [self showComplete];
  1462. }
  1463. if (completionHandler) {
  1464. completionHandler();
  1465. }
  1466. }
  1467. }
  1468. - (void)showAnimations {
  1469. self.backgroundView.alpha = self.coverAlpha;
  1470. if (self.style == LGAlertViewStyleAlert || [LGAlertViewHelper isPadAndNotForce:self]) {
  1471. self.scrollView.transform = CGAffineTransformIdentity;
  1472. self.scrollView.alpha = 1.0;
  1473. self.shadowView.transform = CGAffineTransformIdentity;
  1474. self.shadowView.alpha = 1.0;
  1475. self.blurView.transform = CGAffineTransformIdentity;
  1476. self.blurView.alpha = 1.0;
  1477. }
  1478. else {
  1479. self.scrollView.center = self.scrollViewCenterShowed;
  1480. self.shadowView.center = self.scrollViewCenterShowed;
  1481. self.blurView.center = self.scrollViewCenterShowed;
  1482. }
  1483. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  1484. self.cancelButton.center = self.cancelButtonCenterShowed;
  1485. self.shadowCancelView.center = self.cancelButtonCenterShowed;
  1486. self.blurCancelView.center = self.cancelButtonCenterShowed;
  1487. }
  1488. }
  1489. - (void)showComplete {
  1490. if (self.type == LGAlertViewTypeTextFields && self.textFieldsArray.count) {
  1491. [self.textFieldsArray[0] becomeFirstResponder];
  1492. }
  1493. // -----
  1494. [self didShowCallback];
  1495. // -----
  1496. self.view.userInteractionEnabled = YES;
  1497. }
  1498. #pragma mark - Dismiss
  1499. - (void)dismissAnimated:(BOOL)animated completionHandler:(LGAlertViewCompletionHandler)completionHandler {
  1500. if (!self.isShowing) return;
  1501. if (self.window.isHidden) {
  1502. [self dismissComplete];
  1503. return;
  1504. }
  1505. self.view.userInteractionEnabled = NO;
  1506. self.showing = NO;
  1507. [self.view endEditing:YES];
  1508. // -----
  1509. [self willDismissCallback];
  1510. // -----
  1511. if (animated) {
  1512. [LGAlertViewHelper
  1513. animateWithDuration:self.animationDuration
  1514. animations:^(void) {
  1515. [self dismissAnimations];
  1516. // -----
  1517. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDismissAnimationsNotification
  1518. object:self
  1519. userInfo:@{kLGAlertViewAnimationDuration: @(self.animationDuration)}];
  1520. if (self.dismissAnimationsBlock) {
  1521. self.dismissAnimationsBlock(self, self.animationDuration);
  1522. }
  1523. if (self.delegate && [self.delegate respondsToSelector:@selector(dismissAnimationsForAlertView:duration:)]) {
  1524. [self.delegate dismissAnimationsForAlertView:self duration:self.animationDuration];
  1525. }
  1526. }
  1527. completion:^(BOOL finished) {
  1528. [self dismissComplete];
  1529. if (completionHandler) {
  1530. completionHandler();
  1531. }
  1532. }];
  1533. }
  1534. else {
  1535. [self dismissAnimations];
  1536. [self dismissComplete];
  1537. if (completionHandler) {
  1538. completionHandler();
  1539. }
  1540. }
  1541. }
  1542. - (void)dismissAnimated {
  1543. [self dismissAnimated:YES completionHandler:nil];
  1544. }
  1545. - (void)dismiss {
  1546. [self dismissAnimated:NO completionHandler:nil];
  1547. }
  1548. - (void)dismissAnimations {
  1549. self.backgroundView.alpha = 0.0;
  1550. if (self.style == LGAlertViewStyleAlert || [LGAlertViewHelper isPadAndNotForce:self]) {
  1551. CGAffineTransform transform = CGAffineTransformMakeScale(self.finalScale, self.finalScale);
  1552. CGFloat alpha = 0.0;
  1553. self.scrollView.transform = transform;
  1554. self.scrollView.alpha = alpha;
  1555. self.shadowView.transform = transform;
  1556. self.shadowView.alpha = alpha;
  1557. self.blurView.transform = transform;
  1558. self.blurView.alpha = alpha;
  1559. }
  1560. else {
  1561. self.scrollView.center = self.scrollViewCenterHidden;
  1562. self.shadowView.center = self.scrollViewCenterHidden;
  1563. self.blurView.center = self.scrollViewCenterHidden;
  1564. }
  1565. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  1566. self.cancelButton.center = self.cancelButtonCenterHidden;
  1567. self.shadowCancelView.center = self.cancelButtonCenterHidden;
  1568. self.blurCancelView.center = self.cancelButtonCenterHidden;
  1569. }
  1570. }
  1571. - (void)dismissComplete {
  1572. [self removeObservers];
  1573. self.window.hidden = YES;
  1574. // -----
  1575. [self didDismissCallback];
  1576. // -----
  1577. self.view = nil;
  1578. self.viewController = nil;
  1579. self.window = nil;
  1580. self.delegate = nil;
  1581. }
  1582. #pragma mark - Transition
  1583. - (void)transitionToAlertView:(nonnull LGAlertView *)alertView completionHandler:(LGAlertViewCompletionHandler)completionHandler {
  1584. if (![self isAlertViewValid:alertView] || !self.isShowing) return;
  1585. self.view.userInteractionEnabled = NO;
  1586. [alertView showAnimated:NO
  1587. hidden:YES
  1588. completionHandler:^(void) {
  1589. NSTimeInterval duration = 0.3;
  1590. BOOL cancelButtonSelf = [LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButtonTitle;
  1591. BOOL cancelButtonNext = [LGAlertViewHelper isCancelButtonSeparate:alertView] && alertView.cancelButtonTitle;
  1592. // -----
  1593. [UIView animateWithDuration:duration
  1594. animations:^(void) {
  1595. self.scrollView.alpha = 0.0;
  1596. if (cancelButtonSelf) {
  1597. self.cancelButton.alpha = 0.0;
  1598. if (!cancelButtonNext) {
  1599. self.shadowCancelView.alpha = 0.0;
  1600. self.blurCancelView.alpha = 0.0;
  1601. }
  1602. }
  1603. }
  1604. completion:^(BOOL finished) {
  1605. alertView.backgroundView.alpha = 0.0;
  1606. alertView.backgroundView.hidden = NO;
  1607. [UIView animateWithDuration:duration * 2.0
  1608. animations:^(void) {
  1609. self.backgroundView.alpha = 0.0;
  1610. alertView.backgroundView.alpha = alertView.coverAlpha;
  1611. }];
  1612. // -----
  1613. CGRect shadowViewFrame = alertView.shadowView.frame;
  1614. alertView.shadowView.frame = self.shadowView.frame;
  1615. alertView.shadowView.hidden = NO;
  1616. self.shadowView.hidden = YES;
  1617. // -----
  1618. CGRect blurViewFrame = alertView.blurView.frame;
  1619. alertView.blurView.frame = self.blurView.frame;
  1620. alertView.blurView.hidden = NO;
  1621. self.blurView.hidden = YES;
  1622. // -----
  1623. if (cancelButtonNext) {
  1624. alertView.shadowCancelView.hidden = NO;
  1625. alertView.blurCancelView.hidden = NO;
  1626. if (!cancelButtonSelf) {
  1627. alertView.shadowCancelView.alpha = 0.0;
  1628. alertView.blurCancelView.alpha = 0.0;
  1629. }
  1630. }
  1631. // -----
  1632. if (cancelButtonSelf && cancelButtonNext) {
  1633. self.shadowCancelView.hidden = YES;
  1634. self.blurCancelView.hidden = YES;
  1635. }
  1636. // -----
  1637. [UIView animateWithDuration:duration
  1638. animations:^(void) {
  1639. alertView.shadowView.frame = shadowViewFrame;
  1640. alertView.blurView.frame = blurViewFrame;
  1641. }
  1642. completion:^(BOOL finished) {
  1643. alertView.scrollView.alpha = 0.0;
  1644. alertView.scrollView.hidden = NO;
  1645. if (cancelButtonNext) {
  1646. alertView.cancelButton.alpha = 0.0;
  1647. alertView.cancelButton.hidden = NO;
  1648. }
  1649. [UIView animateWithDuration:duration
  1650. animations:^(void) {
  1651. self.scrollView.alpha = 0.0;
  1652. alertView.scrollView.alpha = 1.0;
  1653. if (cancelButtonNext) {
  1654. alertView.cancelButton.alpha = 1.0;
  1655. if (!cancelButtonSelf) {
  1656. alertView.shadowCancelView.alpha = 1.0;
  1657. alertView.blurCancelView.alpha = 1.0;
  1658. }
  1659. }
  1660. }
  1661. completion:^(BOOL finished) {
  1662. [self dismissAnimated:NO
  1663. completionHandler:^(void) {
  1664. [alertView showComplete];
  1665. if (completionHandler) {
  1666. completionHandler();
  1667. }
  1668. }];
  1669. }];
  1670. }];
  1671. }];
  1672. }];
  1673. }
  1674. - (void)transitionToAlertView:(LGAlertView *)alertView {
  1675. [self transitionToAlertView:alertView completionHandler:nil];
  1676. }
  1677. #pragma mark -
  1678. - (void)subviewsValidateWithSize:(CGSize)size {
  1679. if (CGSizeEqualToSize(size, CGSizeZero)) {
  1680. size = self.viewController.view.bounds.size;
  1681. }
  1682. // -----
  1683. CGFloat width = self.width;
  1684. // -----
  1685. if (!self.isExists) {
  1686. self.exists = YES;
  1687. self.backgroundView.backgroundColor = self.coverColor;
  1688. self.backgroundView.effect = self.coverBlurEffect;
  1689. self.shadowView = [LGAlertViewShadowView new];
  1690. self.shadowView.clipsToBounds = YES;
  1691. self.shadowView.userInteractionEnabled = NO;
  1692. self.shadowView.cornerRadius = self.layerCornerRadius;
  1693. self.shadowView.strokeColor = self.layerBorderColor;
  1694. self.shadowView.strokeWidth = self.layerBorderWidth;
  1695. self.shadowView.shadowColor = self.layerShadowColor;
  1696. self.shadowView.shadowBlur = self.layerShadowRadius;
  1697. self.shadowView.shadowOffset = self.layerShadowOffset;
  1698. [self.view addSubview:self.shadowView];
  1699. self.blurView = [[UIVisualEffectView alloc] initWithEffect:self.backgroundBlurEffect];
  1700. self.blurView.contentView.backgroundColor = self.backgroundColor;
  1701. self.blurView.clipsToBounds = YES;
  1702. self.blurView.layer.cornerRadius = self.layerCornerRadius;
  1703. self.blurView.layer.borderWidth = self.layerBorderWidth;
  1704. self.blurView.layer.borderColor = self.layerBorderColor.CGColor;
  1705. self.blurView.userInteractionEnabled = NO;
  1706. [self.view addSubview:self.blurView];
  1707. self.scrollView = [UIScrollView new];
  1708. self.scrollView.backgroundColor = UIColor.clearColor;
  1709. self.scrollView.indicatorStyle = self.indicatorStyle;
  1710. self.scrollView.showsVerticalScrollIndicator = self.showsVerticalScrollIndicator;
  1711. self.scrollView.alwaysBounceVertical = NO;
  1712. self.scrollView.clipsToBounds = YES;
  1713. self.scrollView.layer.cornerRadius = self.layerCornerRadius - self.layerBorderWidth - (self.layerBorderWidth ? 1.0 : 0.0);
  1714. [self.view addSubview:self.scrollView];
  1715. CGFloat offsetY = 0.0;
  1716. if (self.title) {
  1717. self.titleLabel = [UILabel new];
  1718. self.titleLabel.text = self.title;
  1719. self.titleLabel.textColor = self.titleTextColor;
  1720. self.titleLabel.textAlignment = self.titleTextAlignment;
  1721. self.titleLabel.numberOfLines = 0;
  1722. self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
  1723. self.titleLabel.backgroundColor = UIColor.clearColor;
  1724. self.titleLabel.font = self.titleFont;
  1725. CGSize titleLabelSize = [self.titleLabel sizeThatFits:CGSizeMake(width - LGAlertViewPaddingWidth * 2.0, CGFLOAT_MAX)];
  1726. CGRect titleLabelFrame = CGRectMake(LGAlertViewPaddingWidth, self.innerMarginHeight, width - LGAlertViewPaddingWidth * 2.0, titleLabelSize.height);
  1727. if (LGAlertViewHelper.isNotRetina) {
  1728. titleLabelFrame = CGRectIntegral(titleLabelFrame);
  1729. }
  1730. self.titleLabel.frame = titleLabelFrame;
  1731. [self.scrollView addSubview:self.titleLabel];
  1732. offsetY = CGRectGetMinY(self.titleLabel.frame) + CGRectGetHeight(self.titleLabel.frame);
  1733. }
  1734. if (self.message) {
  1735. self.messageLabel = [UILabel new];
  1736. self.messageLabel.text = self.message;
  1737. self.messageLabel.textColor = self.messageTextColor;
  1738. self.messageLabel.textAlignment = self.messageTextAlignment;
  1739. self.messageLabel.numberOfLines = 0;
  1740. self.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
  1741. self.messageLabel.backgroundColor = UIColor.clearColor;
  1742. self.messageLabel.font = self.messageFont;
  1743. if (!offsetY) {
  1744. offsetY = self.innerMarginHeight / 2.0;
  1745. }
  1746. else if (self.style == LGAlertViewStyleActionSheet) {
  1747. offsetY -= self.innerMarginHeight / 3.0;
  1748. }
  1749. CGSize messageLabelSize = [self.messageLabel sizeThatFits:CGSizeMake(width - LGAlertViewPaddingWidth * 2.0, CGFLOAT_MAX)];
  1750. CGRect messageLabelFrame = CGRectMake(LGAlertViewPaddingWidth, offsetY + self.innerMarginHeight / 2.0, width-LGAlertViewPaddingWidth * 2.0, messageLabelSize.height);
  1751. if (LGAlertViewHelper.isNotRetina) {
  1752. messageLabelFrame = CGRectIntegral(messageLabelFrame);
  1753. }
  1754. self.messageLabel.frame = messageLabelFrame;
  1755. [self.scrollView addSubview:self.messageLabel];
  1756. offsetY = CGRectGetMinY(self.messageLabel.frame) + CGRectGetHeight(self.messageLabel.frame);
  1757. }
  1758. if (self.innerView) {
  1759. self.innerContainerView = [UIView new];
  1760. self.innerContainerView.backgroundColor = UIColor.clearColor;
  1761. CGRect innerContainerViewFrame = CGRectMake(0.0, offsetY + self.innerMarginHeight, width, CGRectGetHeight(self.innerView.bounds));
  1762. if (LGAlertViewHelper.isNotRetina) {
  1763. innerContainerViewFrame = CGRectIntegral(innerContainerViewFrame);
  1764. }
  1765. self.innerContainerView.frame = innerContainerViewFrame;
  1766. [self.scrollView addSubview:self.innerContainerView];
  1767. CGRect innerViewFrame = CGRectMake((width / 2.0) - (CGRectGetWidth(self.innerView.bounds) / 2.0),
  1768. 0.0,
  1769. CGRectGetWidth(self.innerView.bounds),
  1770. CGRectGetHeight(self.innerView.bounds));
  1771. if (LGAlertViewHelper.isNotRetina) {
  1772. innerViewFrame = CGRectIntegral(innerViewFrame);
  1773. }
  1774. self.innerView.frame = innerViewFrame;
  1775. [self.innerContainerView addSubview:self.innerView];
  1776. offsetY = CGRectGetMinY(self.innerContainerView.frame) + CGRectGetHeight(self.innerContainerView.frame);
  1777. }
  1778. else if (self.type == LGAlertViewTypeActivityIndicator || self.type == LGAlertViewTypeProgressView) {
  1779. if (self.type == LGAlertViewTypeActivityIndicator) {
  1780. self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:self.activityIndicatorViewStyle];
  1781. self.activityIndicator.color = self.activityIndicatorViewColor;
  1782. self.activityIndicator.backgroundColor = UIColor.clearColor;
  1783. [self.activityIndicator startAnimating];
  1784. CGRect activityIndicatorFrame = CGRectMake(width / 2.0 - CGRectGetWidth(self.activityIndicator.bounds) / 2.0,
  1785. offsetY + self.innerMarginHeight,
  1786. CGRectGetWidth(self.activityIndicator.bounds),
  1787. CGRectGetHeight(self.activityIndicator.bounds));
  1788. if (LGAlertViewHelper.isNotRetina) {
  1789. activityIndicatorFrame = CGRectIntegral(activityIndicatorFrame);
  1790. }
  1791. self.activityIndicator.frame = activityIndicatorFrame;
  1792. [self.scrollView addSubview:self.activityIndicator];
  1793. offsetY = CGRectGetMinY(self.activityIndicator.frame) + CGRectGetHeight(self.activityIndicator.frame);
  1794. }
  1795. else if (self.type == LGAlertViewTypeProgressView) {
  1796. self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  1797. self.progressView.progress = self.progress;
  1798. self.progressView.backgroundColor = UIColor.clearColor;
  1799. self.progressView.progressTintColor = self.progressViewProgressTintColor;
  1800. self.progressView.trackTintColor = self.progressViewTrackTintColor;
  1801. if (self.progressViewProgressImage) {
  1802. self.progressView.progressImage = self.progressViewProgressImage;
  1803. }
  1804. if (self.progressViewTrackImage) {
  1805. self.progressView.trackImage = self.progressViewTrackImage;
  1806. }
  1807. CGRect progressViewFrame = CGRectMake(LGAlertViewPaddingWidth,
  1808. offsetY + self.innerMarginHeight,
  1809. width - (LGAlertViewPaddingWidth * 2.0),
  1810. CGRectGetHeight(self.progressView.bounds));
  1811. if (LGAlertViewHelper.isNotRetina) {
  1812. progressViewFrame = CGRectIntegral(progressViewFrame);
  1813. }
  1814. self.progressView.frame = progressViewFrame;
  1815. [self.scrollView addSubview:self.progressView];
  1816. offsetY = CGRectGetMinY(self.progressView.frame) + CGRectGetHeight(self.progressView.frame);
  1817. }
  1818. if (self.progressLabelText) {
  1819. self.progressLabel = [UILabel new];
  1820. self.progressLabel.text = self.progressLabelText;
  1821. self.progressLabel.textColor = self.progressLabelTextColor;
  1822. self.progressLabel.textAlignment = self.progressLabelTextAlignment;
  1823. self.progressLabel.numberOfLines = self.progressLabelNumberOfLines;
  1824. self.progressLabel.backgroundColor = UIColor.clearColor;
  1825. self.progressLabel.font = self.progressLabelFont;
  1826. self.progressLabel.lineBreakMode = self.progressLabelLineBreakMode;
  1827. CGRect progressLabelFrame = CGRectMake(LGAlertViewPaddingWidth,
  1828. offsetY + (self.innerMarginHeight / 2.0),
  1829. width - (LGAlertViewPaddingWidth * 2.0),
  1830. self.progressLabelNumberOfLines * self.progressLabelFont.lineHeight);
  1831. if (LGAlertViewHelper.isNotRetina) {
  1832. progressLabelFrame = CGRectIntegral(progressLabelFrame);
  1833. }
  1834. self.progressLabel.frame = progressLabelFrame;
  1835. [self.scrollView addSubview:self.progressLabel];
  1836. offsetY = CGRectGetMinY(self.progressLabel.frame) + CGRectGetHeight(self.progressLabel.frame);
  1837. }
  1838. }
  1839. else if (self.type == LGAlertViewTypeTextFields) {
  1840. NSMutableArray *textFieldsArray = [NSMutableArray new];
  1841. for (NSUInteger i = 0; i < self.numberOfTextFields; i++) {
  1842. UIView *separatorView = [UIView new];
  1843. separatorView.backgroundColor = self.separatorsColor;
  1844. CGRect separatorViewFrame = CGRectMake(0.0,
  1845. offsetY + (i == 0 ? self.innerMarginHeight : 0.0),
  1846. width,
  1847. LGAlertViewHelper.separatorHeight);
  1848. if (LGAlertViewHelper.isNotRetina) {
  1849. separatorViewFrame = CGRectIntegral(separatorViewFrame);
  1850. }
  1851. separatorView.frame = separatorViewFrame;
  1852. [self.scrollView addSubview:separatorView];
  1853. offsetY = CGRectGetMinY(separatorView.frame) + CGRectGetHeight(separatorView.frame);
  1854. // -----
  1855. LGAlertViewTextField *textField = [LGAlertViewTextField new];
  1856. textField.delegate = self;
  1857. textField.tag = i;
  1858. textField.backgroundColor = self.textFieldsBackgroundColor;
  1859. textField.textColor = self.textFieldsTextColor;
  1860. textField.font = self.textFieldsFont;
  1861. textField.textAlignment = self.textFieldsTextAlignment;
  1862. textField.clearsOnBeginEditing = self.textFieldsClearsOnBeginEditing;
  1863. textField.adjustsFontSizeToFitWidth = self.textFieldsAdjustsFontSizeToFitWidth;
  1864. textField.minimumFontSize = self.textFieldsMinimumFontSize;
  1865. textField.clearButtonMode = self.textFieldsClearButtonMode;
  1866. if (i == self.numberOfTextFields - 1) {
  1867. textField.returnKeyType = UIReturnKeyDone;
  1868. }
  1869. else {
  1870. textField.returnKeyType = UIReturnKeyNext;
  1871. }
  1872. if (self.textFieldsSetupHandler) {
  1873. self.textFieldsSetupHandler(textField, i);
  1874. }
  1875. CGRect textFieldFrame = CGRectMake(0.0, offsetY, width, self.textFieldsHeight);
  1876. if (LGAlertViewHelper.isNotRetina) {
  1877. textFieldFrame = CGRectIntegral(textFieldFrame);
  1878. }
  1879. textField.frame = textFieldFrame;
  1880. [self.scrollView addSubview:textField];
  1881. [textFieldsArray addObject:textField];
  1882. offsetY = CGRectGetMinY(textField.frame) + CGRectGetHeight(textField.frame);
  1883. }
  1884. self.textFieldsArray = textFieldsArray;
  1885. offsetY -= self.innerMarginHeight;
  1886. }
  1887. // -----
  1888. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButtonTitle) {
  1889. self.shadowCancelView = [LGAlertViewShadowView new];
  1890. self.shadowCancelView.clipsToBounds = YES;
  1891. self.shadowCancelView.userInteractionEnabled = NO;
  1892. self.shadowCancelView.cornerRadius = self.layerCornerRadius;
  1893. self.shadowCancelView.strokeColor = self.layerBorderColor;
  1894. self.shadowCancelView.strokeWidth = self.layerBorderWidth;
  1895. self.shadowCancelView.shadowColor = self.layerShadowColor;
  1896. self.shadowCancelView.shadowBlur = self.layerShadowRadius;
  1897. self.shadowCancelView.shadowOffset = self.layerShadowOffset;
  1898. [self.view insertSubview:self.shadowCancelView belowSubview:self.scrollView];
  1899. self.blurCancelView = [[UIVisualEffectView alloc] initWithEffect:self.backgroundBlurEffect];
  1900. self.blurCancelView.contentView.backgroundColor = self.backgroundColor;
  1901. self.blurCancelView.clipsToBounds = YES;
  1902. self.blurCancelView.layer.cornerRadius = self.layerCornerRadius;
  1903. self.blurCancelView.layer.borderWidth = self.layerBorderWidth;
  1904. self.blurCancelView.layer.borderColor = self.layerBorderColor.CGColor;
  1905. self.blurCancelView.userInteractionEnabled = NO;
  1906. [self.view insertSubview:self.blurCancelView aboveSubview:self.shadowCancelView];
  1907. [self cancelButtonInit];
  1908. self.cancelButton.layer.masksToBounds = YES;
  1909. self.cancelButton.layer.cornerRadius = self.layerCornerRadius - self.layerBorderWidth - (self.layerBorderWidth ? 1.0 : 0.0);
  1910. [self.view insertSubview:self.cancelButton aboveSubview:self.blurCancelView];
  1911. }
  1912. // -----
  1913. NSUInteger numberOfButtons = self.buttonTitles.count;
  1914. if (self.destructiveButtonTitle) {
  1915. numberOfButtons++;
  1916. }
  1917. if (self.cancelButtonTitle && ![LGAlertViewHelper isCancelButtonSeparate:self]) {
  1918. numberOfButtons++;
  1919. }
  1920. BOOL showTable = NO;
  1921. if (numberOfButtons) {
  1922. if (!self.isOneRowOneButton &&
  1923. ((self.style == LGAlertViewStyleAlert && numberOfButtons < 4) ||
  1924. (self.style == LGAlertViewStyleActionSheet && numberOfButtons == 1))) {
  1925. CGFloat buttonWidth = width/numberOfButtons;
  1926. if (buttonWidth < 64.0) {
  1927. showTable = YES;
  1928. }
  1929. if (self.destructiveButtonTitle && !showTable) {
  1930. self.destructiveButton = [LGAlertViewButton new];
  1931. self.destructiveButton.titleLabel.numberOfLines = self.destructiveButtonNumberOfLines;
  1932. self.destructiveButton.titleLabel.lineBreakMode = self.destructiveButtonLineBreakMode;
  1933. self.destructiveButton.titleLabel.adjustsFontSizeToFitWidth = self.destructiveButtonAdjustsFontSizeToFitWidth;
  1934. self.destructiveButton.titleLabel.minimumScaleFactor = self.destructiveButtonMinimumScaleFactor;
  1935. self.destructiveButton.titleLabel.font = self.destructiveButtonFont;
  1936. self.destructiveButton.titleLabel.textAlignment = self.destructiveButtonTextAlignment;
  1937. self.destructiveButton.iconPosition = self.destructiveButtonIconPosition;
  1938. [self.destructiveButton setTitle:self.destructiveButtonTitle forState:UIControlStateNormal];
  1939. [self.destructiveButton setTitleColor:self.destructiveButtonTitleColor forState:UIControlStateNormal];
  1940. [self.destructiveButton setTitleColor:self.destructiveButtonTitleColorHighlighted forState:UIControlStateHighlighted];
  1941. [self.destructiveButton setTitleColor:self.destructiveButtonTitleColorHighlighted forState:UIControlStateSelected];
  1942. [self.destructiveButton setTitleColor:self.destructiveButtonTitleColorDisabled forState:UIControlStateDisabled];
  1943. [self.destructiveButton setBackgroundColor:self.destructiveButtonBackgroundColor forState:UIControlStateNormal];
  1944. [self.destructiveButton setBackgroundColor:self.destructiveButtonBackgroundColorHighlighted forState:UIControlStateHighlighted];
  1945. [self.destructiveButton setBackgroundColor:self.destructiveButtonBackgroundColorHighlighted forState:UIControlStateSelected];
  1946. [self.destructiveButton setBackgroundColor:self.destructiveButtonBackgroundColorDisabled forState:UIControlStateDisabled];
  1947. [self.destructiveButton setImage:self.destructiveButtonIconImage forState:UIControlStateNormal];
  1948. [self.destructiveButton setImage:self.destructiveButtonIconImageHighlighted forState:UIControlStateHighlighted];
  1949. [self.destructiveButton setImage:self.destructiveButtonIconImageHighlighted forState:UIControlStateSelected];
  1950. [self.destructiveButton setImage:self.destructiveButtonIconImageDisabled forState:UIControlStateDisabled];
  1951. if (self.destructiveButtonTextAlignment == NSTextAlignmentLeft) {
  1952. self.destructiveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  1953. }
  1954. else if (self.destructiveButtonTextAlignment == NSTextAlignmentRight) {
  1955. self.destructiveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  1956. }
  1957. if (self.destructiveButton.imageView.image && self.destructiveButton.titleLabel.text.length) {
  1958. self.destructiveButton.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  1959. LGAlertViewButtonImageOffsetFromTitle / 2.0,
  1960. 0.0,
  1961. LGAlertViewButtonImageOffsetFromTitle / 2.0);
  1962. }
  1963. self.destructiveButton.enabled = self.destructiveButtonEnabled;
  1964. [self.destructiveButton addTarget:self action:@selector(destructiveAction:) forControlEvents:UIControlEventTouchUpInside];
  1965. CGSize size = [self.destructiveButton sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
  1966. if (size.width > buttonWidth) {
  1967. showTable = YES;
  1968. }
  1969. }
  1970. if (self.cancelButtonTitle && ![LGAlertViewHelper isCancelButtonSeparate:self] && !showTable) {
  1971. [self cancelButtonInit];
  1972. CGSize size = [self.cancelButton sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
  1973. if (size.width > buttonWidth) {
  1974. showTable = YES;
  1975. }
  1976. }
  1977. if (self.buttonTitles.count > 0 && !showTable) {
  1978. LGAlertViewButtonProperties *properties = nil;
  1979. if (self.buttonsPropertiesDictionary) {
  1980. properties = self.buttonsPropertiesDictionary[@0];
  1981. }
  1982. self.firstButton = [LGAlertViewButton new];
  1983. self.firstButton.titleLabel.numberOfLines = (properties.isUserNumberOfLines ? properties.numberOfLines : self.buttonsNumberOfLines);
  1984. self.firstButton.titleLabel.lineBreakMode = (properties.isUserLineBreakMode ? properties.lineBreakMode : self.buttonsLineBreakMode);
  1985. self.firstButton.titleLabel.adjustsFontSizeToFitWidth = (properties.isAdjustsFontSizeToFitWidth ? properties.adjustsFontSizeToFitWidth : self.buttonsAdjustsFontSizeToFitWidth);
  1986. self.firstButton.titleLabel.minimumScaleFactor = (properties.isUserMinimimScaleFactor ? properties.minimumScaleFactor : self.buttonsMinimumScaleFactor);
  1987. self.firstButton.titleLabel.font = (properties.isUserFont ? properties.font : self.buttonsFont);
  1988. self.firstButton.titleLabel.textAlignment = (properties.isUserTextAlignment ? properties.textAlignment : self.buttonsTextAlignment);
  1989. self.firstButton.iconPosition = (properties.isUserIconPosition ? properties.iconPosition : self.buttonsIconPosition);
  1990. [self.firstButton setTitle:self.buttonTitles[0] forState:UIControlStateNormal];
  1991. [self.firstButton setTitleColor:(properties.isUserTitleColor ? properties.titleColor : self.buttonsTitleColor)
  1992. forState:UIControlStateNormal];
  1993. [self.firstButton setTitleColor:(properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted)
  1994. forState:UIControlStateHighlighted];
  1995. [self.firstButton setTitleColor:(properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted)
  1996. forState:UIControlStateSelected];
  1997. [self.firstButton setTitleColor:(properties.isUserTitleColorDisabled ? properties.titleColorDisabled : self.buttonsTitleColorDisabled)
  1998. forState:UIControlStateDisabled];
  1999. [self.firstButton setBackgroundColor:(properties.isUserBackgroundColor ? properties.backgroundColor : self.buttonsBackgroundColor)
  2000. forState:UIControlStateNormal];
  2001. [self.firstButton setBackgroundColor:(properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted)
  2002. forState:UIControlStateHighlighted];
  2003. [self.firstButton setBackgroundColor:(properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted)
  2004. forState:UIControlStateSelected];
  2005. [self.firstButton setBackgroundColor:(properties.isUserBackgroundColorDisabled ? properties.backgroundColorDisabled : self.buttonsBackgroundColorDisabled)
  2006. forState:UIControlStateDisabled];
  2007. UIImage *image = nil;
  2008. if (properties.isUserIconImage) {
  2009. image = properties.iconImage;
  2010. }
  2011. else if (self.buttonsIconImages.count > 0) {
  2012. image = self.buttonsIconImages[0];
  2013. }
  2014. [self.firstButton setImage:image forState:UIControlStateNormal];
  2015. UIImage *imageHighlighted = nil;
  2016. if (properties.isUserIconImageHighlighted) {
  2017. imageHighlighted = properties.iconImageHighlighted;
  2018. }
  2019. else if (self.buttonsIconImagesHighlighted.count > 0) {
  2020. imageHighlighted = self.buttonsIconImagesHighlighted[0];
  2021. }
  2022. [self.firstButton setImage:imageHighlighted forState:UIControlStateHighlighted];
  2023. [self.firstButton setImage:imageHighlighted forState:UIControlStateSelected];
  2024. UIImage *imageDisabled = nil;
  2025. if (properties.isUserIconImageDisabled) {
  2026. imageDisabled = properties.iconImageDisabled;
  2027. }
  2028. else if (self.buttonsIconImagesDisabled.count > 0) {
  2029. imageDisabled = self.buttonsIconImagesDisabled[0];
  2030. }
  2031. [self.firstButton setImage:imageDisabled forState:UIControlStateDisabled];
  2032. if (self.firstButton.titleLabel.textAlignment == NSTextAlignmentLeft) {
  2033. self.firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  2034. }
  2035. else if (self.firstButton.titleLabel.textAlignment == NSTextAlignmentRight) {
  2036. self.firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  2037. }
  2038. if (self.firstButton.imageView.image && self.firstButton.titleLabel.text.length) {
  2039. self.firstButton.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  2040. LGAlertViewButtonImageOffsetFromTitle / 2.0,
  2041. 0.0,
  2042. LGAlertViewButtonImageOffsetFromTitle / 2.0);
  2043. }
  2044. self.firstButton.enabled = [self.buttonsEnabledArray[0] boolValue];
  2045. [self.firstButton addTarget:self action:@selector(firstButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  2046. CGSize size = [self.firstButton sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
  2047. if (size.width > buttonWidth) {
  2048. showTable = YES;
  2049. }
  2050. if (self.buttonTitles.count > 1 && !showTable) {
  2051. LGAlertViewButtonProperties *properties = nil;
  2052. if (self.buttonsPropertiesDictionary) {
  2053. properties = self.buttonsPropertiesDictionary[@1];
  2054. }
  2055. self.secondButton = [LGAlertViewButton new];
  2056. self.secondButton.titleLabel.numberOfLines = (properties.isUserNumberOfLines ? properties.numberOfLines : self.buttonsNumberOfLines);
  2057. self.secondButton.titleLabel.lineBreakMode = (properties.isUserLineBreakMode ? properties.lineBreakMode : self.buttonsLineBreakMode);
  2058. self.secondButton.titleLabel.adjustsFontSizeToFitWidth = (properties.isAdjustsFontSizeToFitWidth ? properties.adjustsFontSizeToFitWidth : self.buttonsAdjustsFontSizeToFitWidth);
  2059. self.secondButton.titleLabel.minimumScaleFactor = (properties.isUserMinimimScaleFactor ? properties.minimumScaleFactor : self.buttonsMinimumScaleFactor);
  2060. self.secondButton.titleLabel.font = (properties.isUserFont ? properties.font : self.buttonsFont);
  2061. self.secondButton.titleLabel.textAlignment = (properties.isUserTextAlignment ? properties.textAlignment : self.buttonsTextAlignment);
  2062. self.secondButton.iconPosition = (properties.isUserIconPosition ? properties.iconPosition : self.buttonsIconPosition);
  2063. [self.secondButton setTitle:self.buttonTitles[1] forState:UIControlStateNormal];
  2064. [self.secondButton setTitleColor:(properties.isUserTitleColor ? properties.titleColor : self.buttonsTitleColor)
  2065. forState:UIControlStateNormal];
  2066. [self.secondButton setTitleColor:(properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted)
  2067. forState:UIControlStateHighlighted];
  2068. [self.secondButton setTitleColor:(properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted)
  2069. forState:UIControlStateSelected];
  2070. [self.secondButton setTitleColor:(properties.isUserTitleColorDisabled ? properties.titleColorDisabled : self.buttonsTitleColorDisabled)
  2071. forState:UIControlStateDisabled];
  2072. [self.secondButton setBackgroundColor:(properties.isUserBackgroundColor ? properties.backgroundColor : self.buttonsBackgroundColor)
  2073. forState:UIControlStateNormal];
  2074. [self.secondButton setBackgroundColor:(properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted)
  2075. forState:UIControlStateHighlighted];
  2076. [self.secondButton setBackgroundColor:(properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted)
  2077. forState:UIControlStateSelected];
  2078. [self.secondButton setBackgroundColor:(properties.isUserBackgroundColorDisabled ? properties.backgroundColorDisabled : self.buttonsBackgroundColorDisabled)
  2079. forState:UIControlStateDisabled];
  2080. UIImage *image = nil;
  2081. if (properties.isUserIconImage) {
  2082. image = properties.iconImage;
  2083. }
  2084. else if (self.buttonsIconImages.count > 1) {
  2085. image = self.buttonsIconImages[1];
  2086. }
  2087. [self.secondButton setImage:image forState:UIControlStateNormal];
  2088. UIImage *imageHighlighted = nil;
  2089. if (properties.isUserIconImageHighlighted) {
  2090. imageHighlighted = properties.iconImageHighlighted;
  2091. }
  2092. else if (self.buttonsIconImagesHighlighted.count > 1) {
  2093. imageHighlighted = self.buttonsIconImagesHighlighted[1];
  2094. }
  2095. [self.secondButton setImage:imageHighlighted forState:UIControlStateHighlighted];
  2096. [self.secondButton setImage:imageHighlighted forState:UIControlStateSelected];
  2097. UIImage *imageDisabled = nil;
  2098. if (properties.isUserIconImageDisabled) {
  2099. imageDisabled = properties.iconImageDisabled;
  2100. }
  2101. else if (self.buttonsIconImagesDisabled.count > 1) {
  2102. imageDisabled = self.buttonsIconImagesDisabled[1];
  2103. }
  2104. [self.secondButton setImage:imageDisabled forState:UIControlStateDisabled];
  2105. if (self.secondButton.titleLabel.textAlignment == NSTextAlignmentLeft) {
  2106. self.secondButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  2107. }
  2108. else if (self.secondButton.titleLabel.textAlignment == NSTextAlignmentRight) {
  2109. self.secondButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  2110. }
  2111. if (self.secondButton.imageView.image && self.secondButton.titleLabel.text.length) {
  2112. self.secondButton.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  2113. LGAlertViewButtonImageOffsetFromTitle / 2.0,
  2114. 0.0,
  2115. LGAlertViewButtonImageOffsetFromTitle / 2.0);
  2116. }
  2117. self.secondButton.enabled = [self.buttonsEnabledArray[1] boolValue];
  2118. [self.secondButton addTarget:self action:@selector(secondButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  2119. CGSize size = [self.secondButton sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
  2120. if (size.width > buttonWidth) {
  2121. showTable = YES;
  2122. }
  2123. if (self.buttonTitles.count > 2 && !showTable) {
  2124. LGAlertViewButtonProperties *properties = nil;
  2125. if (self.buttonsPropertiesDictionary) {
  2126. properties = self.buttonsPropertiesDictionary[@2];
  2127. }
  2128. self.thirdButton = [LGAlertViewButton new];
  2129. self.thirdButton.titleLabel.numberOfLines = (properties.isUserNumberOfLines ? properties.numberOfLines : self.buttonsNumberOfLines);
  2130. self.thirdButton.titleLabel.lineBreakMode = (properties.isUserLineBreakMode ? properties.lineBreakMode : self.buttonsLineBreakMode);
  2131. self.thirdButton.titleLabel.adjustsFontSizeToFitWidth = (properties.isAdjustsFontSizeToFitWidth ? properties.adjustsFontSizeToFitWidth : self.buttonsAdjustsFontSizeToFitWidth);
  2132. self.thirdButton.titleLabel.minimumScaleFactor = (properties.isUserMinimimScaleFactor ? properties.minimumScaleFactor : self.buttonsMinimumScaleFactor);
  2133. self.thirdButton.titleLabel.font = (properties.isUserFont ? properties.font : self.buttonsFont);
  2134. self.thirdButton.titleLabel.textAlignment = (properties.isUserTextAlignment ? properties.textAlignment : self.buttonsTextAlignment);
  2135. self.thirdButton.iconPosition = (properties.isUserIconPosition ? properties.iconPosition : self.buttonsIconPosition);
  2136. [self.thirdButton setTitle:self.buttonTitles[2] forState:UIControlStateNormal];
  2137. [self.thirdButton setTitleColor:(properties.isUserTitleColor ? properties.titleColor : self.buttonsTitleColor)
  2138. forState:UIControlStateNormal];
  2139. [self.thirdButton setTitleColor:(properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted)
  2140. forState:UIControlStateHighlighted];
  2141. [self.thirdButton setTitleColor:(properties.isUserTitleColorHighlighted ? properties.titleColorHighlighted : self.buttonsTitleColorHighlighted)
  2142. forState:UIControlStateSelected];
  2143. [self.thirdButton setTitleColor:(properties.isUserTitleColorDisabled ? properties.titleColorDisabled : self.buttonsTitleColorDisabled)
  2144. forState:UIControlStateDisabled];
  2145. [self.thirdButton setBackgroundColor:(properties.isUserBackgroundColor ? properties.backgroundColor : self.buttonsBackgroundColor)
  2146. forState:UIControlStateNormal];
  2147. [self.thirdButton setBackgroundColor:(properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted)
  2148. forState:UIControlStateHighlighted];
  2149. [self.thirdButton setBackgroundColor:(properties.isUserBackgroundColorHighlighted ? properties.backgroundColorHighlighted : self.buttonsBackgroundColorHighlighted)
  2150. forState:UIControlStateSelected];
  2151. [self.thirdButton setBackgroundColor:(properties.isUserBackgroundColorDisabled ? properties.backgroundColorDisabled : self.buttonsBackgroundColorDisabled)
  2152. forState:UIControlStateDisabled];
  2153. UIImage *image = nil;
  2154. if (properties.isUserIconImage) {
  2155. image = properties.iconImage;
  2156. }
  2157. else if (self.buttonsIconImages.count > 2) {
  2158. image = self.buttonsIconImages[2];
  2159. }
  2160. [self.thirdButton setImage:image forState:UIControlStateNormal];
  2161. UIImage *imageHighlighted = nil;
  2162. if (properties.isUserIconImageHighlighted) {
  2163. imageHighlighted = properties.iconImageHighlighted;
  2164. }
  2165. else if (self.buttonsIconImagesHighlighted.count > 2) {
  2166. imageHighlighted = self.buttonsIconImagesHighlighted[2];
  2167. }
  2168. [self.thirdButton setImage:imageHighlighted forState:UIControlStateHighlighted];
  2169. [self.thirdButton setImage:imageHighlighted forState:UIControlStateSelected];
  2170. UIImage *imageDisabled = nil;
  2171. if (properties.isUserIconImageDisabled) {
  2172. imageDisabled = properties.iconImageDisabled;
  2173. }
  2174. else if (self.buttonsIconImagesDisabled.count > 2) {
  2175. imageDisabled = self.buttonsIconImagesDisabled[2];
  2176. }
  2177. [self.thirdButton setImage:imageDisabled forState:UIControlStateDisabled];
  2178. if (self.thirdButton.titleLabel.textAlignment == NSTextAlignmentLeft) {
  2179. self.thirdButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  2180. }
  2181. else if (self.thirdButton.titleLabel.textAlignment == NSTextAlignmentRight) {
  2182. self.thirdButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  2183. }
  2184. if (self.thirdButton.imageView.image && self.thirdButton.titleLabel.text.length) {
  2185. self.thirdButton.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  2186. LGAlertViewButtonImageOffsetFromTitle / 2.0,
  2187. 0.0,
  2188. LGAlertViewButtonImageOffsetFromTitle / 2.0);
  2189. }
  2190. self.thirdButton.enabled = [self.buttonsEnabledArray[2] boolValue];
  2191. [self.thirdButton addTarget:self action:@selector(thirdButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  2192. CGSize size = [self.thirdButton sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
  2193. if (size.width > buttonWidth) {
  2194. showTable = YES;
  2195. }
  2196. }
  2197. }
  2198. }
  2199. if (!showTable) {
  2200. UIButton *firstButton = nil;
  2201. UIButton *secondButton = nil;
  2202. UIButton *thirdButton = nil;
  2203. if (self.cancelButton && ![LGAlertViewHelper isCancelButtonSeparate:self]) {
  2204. [self.scrollView addSubview:self.cancelButton];
  2205. firstButton = self.cancelButton;
  2206. }
  2207. if (self.destructiveButton) {
  2208. [self.scrollView addSubview:self.destructiveButton];
  2209. if (!firstButton) {
  2210. firstButton = self.destructiveButton;
  2211. }
  2212. else {
  2213. secondButton = self.destructiveButton;
  2214. }
  2215. }
  2216. if (self.firstButton) {
  2217. [self.scrollView addSubview:self.firstButton];
  2218. if (!firstButton) {
  2219. firstButton = self.firstButton;
  2220. }
  2221. else if (!secondButton) {
  2222. secondButton = self.firstButton;
  2223. }
  2224. else {
  2225. thirdButton = self.firstButton;
  2226. }
  2227. if (self.secondButton) {
  2228. [self.scrollView addSubview:self.secondButton];
  2229. if (!secondButton) {
  2230. secondButton = self.secondButton;
  2231. }
  2232. else {
  2233. thirdButton = self.secondButton;
  2234. }
  2235. if (self.thirdButton) {
  2236. [self.scrollView addSubview:self.thirdButton];
  2237. thirdButton = self.thirdButton;
  2238. }
  2239. }
  2240. }
  2241. // -----
  2242. if (offsetY) {
  2243. self.separatorHorizontalView = [UIView new];
  2244. self.separatorHorizontalView.backgroundColor = self.separatorsColor;
  2245. CGRect separatorHorizontalViewFrame = CGRectMake(0.0, offsetY + self.innerMarginHeight, width, LGAlertViewHelper.separatorHeight);
  2246. if (LGAlertViewHelper.isNotRetina) {
  2247. separatorHorizontalViewFrame = CGRectIntegral(separatorHorizontalViewFrame);
  2248. }
  2249. self.separatorHorizontalView.frame = separatorHorizontalViewFrame;
  2250. [self.scrollView addSubview:self.separatorHorizontalView];
  2251. offsetY = CGRectGetMinY(self.separatorHorizontalView.frame) + CGRectGetHeight(self.separatorHorizontalView.frame);
  2252. }
  2253. // -----
  2254. CGRect firstButtonFrame = CGRectMake(0.0, offsetY, width / numberOfButtons, self.buttonsHeight);
  2255. if (LGAlertViewHelper.isNotRetina) {
  2256. firstButtonFrame = CGRectIntegral(firstButtonFrame);
  2257. }
  2258. firstButton.frame = firstButtonFrame;
  2259. CGRect secondButtonFrame = CGRectZero;
  2260. CGRect thirdButtonFrame = CGRectZero;
  2261. if (secondButton) {
  2262. secondButtonFrame = CGRectMake(CGRectGetMinX(firstButtonFrame) + CGRectGetWidth(firstButtonFrame) + LGAlertViewHelper.separatorHeight,
  2263. offsetY,
  2264. (width / numberOfButtons) - LGAlertViewHelper.separatorHeight,
  2265. self.buttonsHeight);
  2266. if (LGAlertViewHelper.isNotRetina) {
  2267. secondButtonFrame = CGRectIntegral(secondButtonFrame);
  2268. }
  2269. secondButton.frame = secondButtonFrame;
  2270. if (thirdButton) {
  2271. thirdButtonFrame = CGRectMake(CGRectGetMinX(secondButtonFrame) + CGRectGetWidth(secondButtonFrame) + LGAlertViewHelper.separatorHeight,
  2272. offsetY,
  2273. (width / numberOfButtons) - LGAlertViewHelper.separatorHeight,
  2274. self.buttonsHeight);
  2275. if (LGAlertViewHelper.isNotRetina) {
  2276. thirdButtonFrame = CGRectIntegral(thirdButtonFrame);
  2277. }
  2278. thirdButton.frame = thirdButtonFrame;
  2279. }
  2280. }
  2281. // -----
  2282. if (secondButton) {
  2283. self.separatorVerticalView1 = [UIView new];
  2284. self.separatorVerticalView1.backgroundColor = self.separatorsColor;
  2285. CGRect separatorVerticalView1Frame = CGRectMake(CGRectGetMinX(firstButtonFrame) + CGRectGetWidth(firstButtonFrame),
  2286. offsetY,
  2287. LGAlertViewHelper.separatorHeight,
  2288. MAX(CGRectGetWidth(UIScreen.mainScreen.bounds), CGRectGetHeight(UIScreen.mainScreen.bounds)));
  2289. if (LGAlertViewHelper.isNotRetina) {
  2290. separatorVerticalView1Frame = CGRectIntegral(separatorVerticalView1Frame);
  2291. }
  2292. self.separatorVerticalView1.frame = separatorVerticalView1Frame;
  2293. [self.scrollView addSubview:self.separatorVerticalView1];
  2294. if (thirdButton) {
  2295. self.separatorVerticalView2 = [UIView new];
  2296. self.separatorVerticalView2.backgroundColor = self.separatorsColor;
  2297. CGRect separatorVerticalView2Frame = CGRectMake(CGRectGetMinX(secondButtonFrame) + CGRectGetWidth(secondButtonFrame),
  2298. offsetY,
  2299. LGAlertViewHelper.separatorHeight,
  2300. MAX(CGRectGetWidth(UIScreen.mainScreen.bounds), CGRectGetHeight(UIScreen.mainScreen.bounds)));
  2301. if (LGAlertViewHelper.isNotRetina) {
  2302. separatorVerticalView2Frame = CGRectIntegral(separatorVerticalView2Frame);
  2303. }
  2304. self.separatorVerticalView2.frame = separatorVerticalView2Frame;
  2305. [self.scrollView addSubview:self.separatorVerticalView2];
  2306. }
  2307. }
  2308. // -----
  2309. offsetY += self.buttonsHeight;
  2310. }
  2311. }
  2312. else {
  2313. showTable = YES;
  2314. }
  2315. if (showTable) {
  2316. if (![LGAlertViewHelper isCancelButtonSeparate:self]) {
  2317. self.cancelButton = nil;
  2318. }
  2319. self.destructiveButton = nil;
  2320. self.firstButton = nil;
  2321. self.secondButton = nil;
  2322. self.thirdButton = nil;
  2323. NSMutableArray *buttonTitles = nil;
  2324. if (self.buttonTitles) {
  2325. buttonTitles = self.buttonTitles.mutableCopy;
  2326. }
  2327. else {
  2328. buttonTitles = [NSMutableArray new];
  2329. }
  2330. if (self.destructiveButtonTitle) {
  2331. [buttonTitles insertObject:self.destructiveButtonTitle atIndex:0];
  2332. }
  2333. if (self.cancelButtonTitle && ![LGAlertViewHelper isCancelButtonSeparate:self]) {
  2334. [buttonTitles addObject:self.cancelButtonTitle];
  2335. }
  2336. self.buttonTitles = buttonTitles;
  2337. self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  2338. self.tableView.clipsToBounds = NO;
  2339. self.tableView.backgroundColor = UIColor.clearColor;
  2340. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  2341. self.tableView.dataSource = self;
  2342. self.tableView.delegate = self;
  2343. self.tableView.scrollEnabled = NO;
  2344. [self.tableView registerClass:[LGAlertViewCell class] forCellReuseIdentifier:@"cell"];
  2345. self.tableView.frame = CGRectMake(0.0, 0.0, width, CGFLOAT_MAX);
  2346. [self.tableView reloadData];
  2347. [self.tableView layoutIfNeeded];
  2348. if (!offsetY) {
  2349. offsetY = -self.innerMarginHeight;
  2350. }
  2351. else {
  2352. self.separatorHorizontalView = [UIView new];
  2353. self.separatorHorizontalView.backgroundColor = self.separatorsColor;
  2354. CGRect separatorTitleViewFrame = CGRectMake(0.0, 0.0, width, LGAlertViewHelper.separatorHeight);
  2355. if (LGAlertViewHelper.isNotRetina) {
  2356. separatorTitleViewFrame = CGRectIntegral(separatorTitleViewFrame);
  2357. }
  2358. self.separatorHorizontalView.frame = separatorTitleViewFrame;
  2359. self.tableView.tableHeaderView = self.separatorHorizontalView;
  2360. }
  2361. CGRect tableViewFrame = CGRectMake(0.0, offsetY + self.innerMarginHeight, width, self.tableView.contentSize.height);
  2362. if (LGAlertViewHelper.isNotRetina) {
  2363. tableViewFrame = CGRectIntegral(tableViewFrame);
  2364. }
  2365. self.tableView.frame = tableViewFrame;
  2366. [self.scrollView addSubview:self.tableView];
  2367. offsetY = CGRectGetMinY(self.tableView.frame) + CGRectGetHeight(self.tableView.frame);
  2368. }
  2369. }
  2370. else {
  2371. offsetY += self.innerMarginHeight;
  2372. }
  2373. // -----
  2374. self.scrollView.contentSize = CGSizeMake(width, offsetY);
  2375. }
  2376. }
  2377. - (void)layoutValidateWithSize:(CGSize)size {
  2378. if (CGSizeEqualToSize(size, CGSizeZero)) {
  2379. size = self.viewController.view.bounds.size;
  2380. }
  2381. // -----
  2382. CGFloat width = self.width;
  2383. // -----
  2384. self.view.frame = CGRectMake(0.0, 0.0, size.width, size.height);
  2385. self.backgroundView.frame = CGRectMake(0.0, 0.0, size.width, size.height);
  2386. // -----
  2387. CGFloat heightMax = size.height - self.keyboardHeight - (self.offsetVertical * 2.0);
  2388. if (self.windowLevel == LGAlertViewWindowLevelBelowStatusBar) {
  2389. heightMax -= LGAlertViewHelper.statusBarHeight;
  2390. }
  2391. if (self.heightMax != NSNotFound && self.heightMax < heightMax) {
  2392. heightMax = self.heightMax;
  2393. }
  2394. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  2395. heightMax -= self.buttonsHeight + self.cancelButtonOffsetY;
  2396. }
  2397. else if (self.cancelOnTouch && !self.cancelButtonTitle && size.width < width + (self.buttonsHeight * 2.0)) {
  2398. heightMax -= self.buttonsHeight * 2.0;
  2399. }
  2400. if (self.scrollView.contentSize.height < heightMax) {
  2401. heightMax = self.scrollView.contentSize.height;
  2402. }
  2403. // -----
  2404. CGRect scrollViewFrame = CGRectZero;
  2405. CGAffineTransform scrollViewTransform = CGAffineTransformIdentity;
  2406. CGFloat scrollViewAlpha = 1.0;
  2407. if (self.style == LGAlertViewStyleAlert || [LGAlertViewHelper isPadAndNotForce:self]) {
  2408. scrollViewFrame = CGRectMake((size.width - width) / 2.0, (size.height - self.keyboardHeight - heightMax) / 2.0, width, heightMax);
  2409. if (self.windowLevel == LGAlertViewWindowLevelBelowStatusBar) {
  2410. scrollViewFrame.origin.y += LGAlertViewHelper.statusBarHeight / 2.0;
  2411. }
  2412. if (!self.isShowing) {
  2413. scrollViewTransform = CGAffineTransformMakeScale(self.initialScale, self.initialScale);
  2414. scrollViewAlpha = 0.0;
  2415. }
  2416. }
  2417. else
  2418. {
  2419. CGFloat bottomShift = self.offsetVertical;
  2420. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  2421. bottomShift += self.buttonsHeight+self.cancelButtonOffsetY;
  2422. }
  2423. scrollViewFrame = CGRectMake((size.width - width) / 2.0, size.height - bottomShift - heightMax, width, heightMax);
  2424. }
  2425. // -----
  2426. if (self.style == LGAlertViewStyleActionSheet && ![LGAlertViewHelper isPadAndNotForce:self]) {
  2427. CGRect cancelButtonFrame = CGRectZero;
  2428. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  2429. cancelButtonFrame = CGRectMake((size.width - width) / 2.0, size.height - self.cancelButtonOffsetY - self.buttonsHeight, width, self.buttonsHeight);
  2430. }
  2431. self.scrollViewCenterShowed = CGPointMake(CGRectGetMinX(scrollViewFrame) + (CGRectGetWidth(scrollViewFrame) / 2.0),
  2432. CGRectGetMinY(scrollViewFrame) + (CGRectGetHeight(scrollViewFrame) / 2.0));
  2433. self.cancelButtonCenterShowed = CGPointMake(CGRectGetMinX(cancelButtonFrame) + (CGRectGetWidth(cancelButtonFrame) / 2.0),
  2434. CGRectGetMinY(cancelButtonFrame) + (CGRectGetHeight(cancelButtonFrame) / 2.0));
  2435. // -----
  2436. CGFloat commonHeight = CGRectGetHeight(scrollViewFrame) + self.offsetVertical;
  2437. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  2438. commonHeight += self.buttonsHeight + self.cancelButtonOffsetY;
  2439. }
  2440. self.scrollViewCenterHidden = CGPointMake(CGRectGetMinX(scrollViewFrame) + (CGRectGetWidth(scrollViewFrame) / 2.0),
  2441. CGRectGetMinY(scrollViewFrame) + (CGRectGetHeight(scrollViewFrame) / 2.0) + commonHeight + self.layerBorderWidth + self.layerShadowRadius);
  2442. self.cancelButtonCenterHidden = CGPointMake(CGRectGetMinX(cancelButtonFrame) + (CGRectGetWidth(cancelButtonFrame) / 2.0),
  2443. CGRectGetMinY(cancelButtonFrame) + (CGRectGetHeight(cancelButtonFrame) / 2.0) + commonHeight);
  2444. if (!self.isShowing) {
  2445. scrollViewFrame.origin.y += commonHeight;
  2446. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  2447. cancelButtonFrame.origin.y += commonHeight;
  2448. }
  2449. }
  2450. // -----
  2451. if ([LGAlertViewHelper isCancelButtonSeparate:self] && self.cancelButton) {
  2452. if (LGAlertViewHelper.isNotRetina) {
  2453. cancelButtonFrame = CGRectIntegral(cancelButtonFrame);
  2454. }
  2455. self.cancelButton.frame = cancelButtonFrame;
  2456. CGFloat offset = self.layerBorderWidth + self.layerShadowRadius;
  2457. self.shadowCancelView.frame = CGRectInset(cancelButtonFrame, -offset, -offset);
  2458. [self.shadowCancelView setNeedsDisplay];
  2459. self.blurCancelView.frame = CGRectInset(cancelButtonFrame, -self.layerBorderWidth, -self.layerBorderWidth);
  2460. }
  2461. }
  2462. // -----
  2463. if (LGAlertViewHelper.isNotRetina) {
  2464. scrollViewFrame = CGRectIntegral(scrollViewFrame);
  2465. if (CGRectGetHeight(scrollViewFrame) - self.scrollView.contentSize.height == 1.0) {
  2466. scrollViewFrame.size.height -= 2.0;
  2467. }
  2468. }
  2469. // -----
  2470. self.scrollView.frame = scrollViewFrame;
  2471. self.scrollView.transform = scrollViewTransform;
  2472. self.scrollView.alpha = scrollViewAlpha;
  2473. // -----
  2474. CGFloat offset = self.layerBorderWidth + self.layerShadowRadius;
  2475. self.shadowView.frame = CGRectInset(scrollViewFrame, -offset, -offset);
  2476. self.shadowView.transform = scrollViewTransform;
  2477. self.shadowView.alpha = scrollViewAlpha;
  2478. [self.shadowView setNeedsDisplay];
  2479. // -----
  2480. self.blurView.frame = CGRectInset(scrollViewFrame, -self.layerBorderWidth, -self.layerBorderWidth);
  2481. self.blurView.transform = scrollViewTransform;
  2482. self.blurView.alpha = scrollViewAlpha;
  2483. }
  2484. - (void)cancelButtonInit {
  2485. self.cancelButton = [LGAlertViewButton new];
  2486. self.cancelButton.titleLabel.numberOfLines = self.cancelButtonNumberOfLines;
  2487. self.cancelButton.titleLabel.lineBreakMode = self.cancelButtonLineBreakMode;
  2488. self.cancelButton.titleLabel.adjustsFontSizeToFitWidth = self.cancelButtonAdjustsFontSizeToFitWidth;
  2489. self.cancelButton.titleLabel.minimumScaleFactor = self.cancelButtonMinimumScaleFactor;
  2490. self.cancelButton.titleLabel.font = self.cancelButtonFont;
  2491. self.cancelButton.titleLabel.textAlignment = self.cancelButtonTextAlignment;
  2492. self.cancelButton.iconPosition = self.cancelButtonIconPosition;
  2493. [self.cancelButton setTitle:self.cancelButtonTitle forState:UIControlStateNormal];
  2494. [self.cancelButton setTitleColor:self.cancelButtonTitleColor forState:UIControlStateNormal];
  2495. [self.cancelButton setTitleColor:self.cancelButtonTitleColorHighlighted forState:UIControlStateHighlighted];
  2496. [self.cancelButton setTitleColor:self.cancelButtonTitleColorHighlighted forState:UIControlStateSelected];
  2497. [self.cancelButton setTitleColor:self.cancelButtonTitleColorDisabled forState:UIControlStateDisabled];
  2498. [self.cancelButton setBackgroundColor:self.cancelButtonBackgroundColor forState:UIControlStateNormal];
  2499. [self.cancelButton setBackgroundColor:self.cancelButtonBackgroundColorHighlighted forState:UIControlStateHighlighted];
  2500. [self.cancelButton setBackgroundColor:self.cancelButtonBackgroundColorHighlighted forState:UIControlStateSelected];
  2501. [self.cancelButton setBackgroundColor:self.cancelButtonBackgroundColorDisabled forState:UIControlStateDisabled];
  2502. [self.cancelButton setImage:self.cancelButtonIconImage forState:UIControlStateNormal];
  2503. [self.cancelButton setImage:self.cancelButtonIconImageHighlighted forState:UIControlStateHighlighted];
  2504. [self.cancelButton setImage:self.cancelButtonIconImageHighlighted forState:UIControlStateSelected];
  2505. [self.cancelButton setImage:self.cancelButtonIconImageDisabled forState:UIControlStateDisabled];
  2506. if (self.cancelButtonTextAlignment == NSTextAlignmentLeft) {
  2507. self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  2508. }
  2509. else if (self.cancelButtonTextAlignment == NSTextAlignmentRight) {
  2510. self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  2511. }
  2512. if (self.cancelButton.imageView.image && self.cancelButton.titleLabel.text.length) {
  2513. self.cancelButton.titleEdgeInsets = UIEdgeInsetsMake(0.0,
  2514. LGAlertViewButtonImageOffsetFromTitle / 2.0,
  2515. 0.0,
  2516. LGAlertViewButtonImageOffsetFromTitle / 2.0);
  2517. }
  2518. self.cancelButton.enabled = self.cancelButtonEnabled;
  2519. [self.cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
  2520. }
  2521. #pragma mark - Actions
  2522. - (void)cancelAction:(id)sender {
  2523. if (sender && [sender isKindOfClass:[UIButton class]]) {
  2524. [(UIButton *)sender setSelected:YES];
  2525. }
  2526. // -----
  2527. if (self.cancelHandler) {
  2528. self.cancelHandler(self);
  2529. }
  2530. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewCancelled:)]) {
  2531. [self.delegate alertViewCancelled:self];
  2532. }
  2533. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewCancelNotification object:self userInfo:nil];
  2534. // -----
  2535. if (self.dismissOnAction) {
  2536. [self dismissAnimated:self.shouldDismissAnimated completionHandler:^{
  2537. if (self.didDismissAfterCancelHandler) {
  2538. self.didDismissAfterCancelHandler(self);
  2539. }
  2540. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewDidDismissAfterCancelled:)]) {
  2541. [self.delegate alertViewDidDismissAfterCancelled:self];
  2542. }
  2543. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDidDismissAfterCancelNotification object:self userInfo:nil];
  2544. }];
  2545. }
  2546. }
  2547. - (void)destructiveAction:(id)sender {
  2548. if (sender && [sender isKindOfClass:[UIButton class]]) {
  2549. [(UIButton *)sender setSelected:YES];
  2550. }
  2551. // -----
  2552. if (self.destructiveHandler) {
  2553. self.destructiveHandler(self);
  2554. }
  2555. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewDestructed:)]) {
  2556. [self.delegate alertViewDestructed:self];
  2557. }
  2558. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDestructiveNotification object:self userInfo:nil];
  2559. // -----
  2560. if (self.dismissOnAction) {
  2561. [self dismissAnimated:self.shouldDismissAnimated completionHandler:^{
  2562. if (self.didDismissAfterDestructiveHandler) {
  2563. self.didDismissAfterDestructiveHandler(self);
  2564. }
  2565. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewDidDismissAfterDestructed:)]) {
  2566. [self.delegate alertViewDidDismissAfterDestructed:self];
  2567. }
  2568. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDidDismissAfterDestructiveNotification object:self userInfo:nil];
  2569. }];
  2570. }
  2571. }
  2572. - (void)actionActionAtIndex:(NSUInteger)index title:(NSString *)title {
  2573. if (self.actionHandler) {
  2574. self.actionHandler(self, index, title);
  2575. }
  2576. if (self.delegate && [self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:title:)]) {
  2577. [self.delegate alertView:self clickedButtonAtIndex:index title:title];
  2578. }
  2579. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewActionNotification
  2580. object:self
  2581. userInfo:@{@"title": title,
  2582. @"index": @(index)}];
  2583. // -----
  2584. if (self.dismissOnAction) {
  2585. [self dismissAnimated:self.shouldDismissAnimated completionHandler:^{
  2586. if (self.didDismissAfterActionHandler) {
  2587. self.didDismissAfterActionHandler(self, index, title);
  2588. }
  2589. if (self.delegate && [self.delegate respondsToSelector:@selector(alertView:didDismissAfterClickedButtonAtIndex:title:)]) {
  2590. [self.delegate alertView:self didDismissAfterClickedButtonAtIndex:index title:title];
  2591. }
  2592. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDidDismissAfterActionNotification
  2593. object:self
  2594. userInfo:@{@"title": title,
  2595. @"index": @(index)}];
  2596. }];
  2597. }
  2598. }
  2599. - (void)firstButtonAction:(id)sender {
  2600. if (sender && [sender isKindOfClass:[UIButton class]]) {
  2601. [(UIButton *)sender setSelected:YES];
  2602. }
  2603. // -----
  2604. NSUInteger index = 0;
  2605. NSString *title = self.buttonTitles[0];
  2606. // -----
  2607. [self actionActionAtIndex:index title:title];
  2608. }
  2609. - (void)secondButtonAction:(id)sender {
  2610. if (sender && [sender isKindOfClass:[UIButton class]]) {
  2611. [(UIButton *)sender setSelected:YES];
  2612. }
  2613. // -----
  2614. NSUInteger index = 1;
  2615. NSString *title = self.buttonTitles[1];
  2616. // -----
  2617. [self actionActionAtIndex:index title:title];
  2618. }
  2619. - (void)thirdButtonAction:(id)sender {
  2620. if (sender && [sender isKindOfClass:[UIButton class]]) {
  2621. [(UIButton *)sender setSelected:YES];
  2622. }
  2623. // -----
  2624. NSUInteger index = 2;
  2625. NSString *title = self.buttonTitles[2];
  2626. // -----
  2627. [self actionActionAtIndex:index title:title];
  2628. }
  2629. #pragma mark - Callbacks
  2630. - (void)willShowCallback {
  2631. if (self.willShowHandler) {
  2632. self.willShowHandler(self);
  2633. }
  2634. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewWillShow:)]) {
  2635. [self.delegate alertViewWillShow:self];
  2636. }
  2637. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewWillShowNotification object:self userInfo:nil];
  2638. }
  2639. - (void)didShowCallback {
  2640. if (self.didShowHandler) {
  2641. self.didShowHandler(self);
  2642. }
  2643. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewDidShow:)]) {
  2644. [self.delegate alertViewDidShow:self];
  2645. }
  2646. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDidShowNotification object:self userInfo:nil];
  2647. }
  2648. - (void)willDismissCallback {
  2649. if (self.willDismissHandler) {
  2650. self.willDismissHandler(self);
  2651. }
  2652. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewWillDismiss:)]) {
  2653. [self.delegate alertViewWillDismiss:self];
  2654. }
  2655. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewWillDismissNotification object:self userInfo:nil];
  2656. }
  2657. - (void)didDismissCallback {
  2658. if (self.didDismissHandler) {
  2659. self.didDismissHandler(self);
  2660. }
  2661. if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewDidDismiss:)]) {
  2662. [self.delegate alertViewDidDismiss:self];
  2663. }
  2664. [[NSNotificationCenter defaultCenter] postNotificationName:LGAlertViewDidDismissNotification object:self userInfo:nil];
  2665. }
  2666. #pragma mark - Helpers
  2667. - (BOOL)isAlertViewValid:(LGAlertView *)alertView {
  2668. NSAssert(alertView.isInitialized, @"You need to use one of \"- initWith...\" or \"+ alertViewWith...\" methods to initialize LGAlertView");
  2669. return YES;
  2670. }
  2671. - (BOOL)isValid {
  2672. return [self isAlertViewValid:self];
  2673. }
  2674. - (CGFloat)innerMarginHeight {
  2675. return self.style == LGAlertViewStyleAlert ? 16.0 : 12.0;
  2676. }
  2677. @end
  2678. #pragma mark - Deprecated
  2679. @implementation LGAlertView (Deprecated)
  2680. - (void)setLayerShadowOpacity:(CGFloat)layerShadowOpacity {
  2681. if (!self.layerShadowColor) return;
  2682. self.layerShadowColor = [self.layerShadowColor colorWithAlphaComponent:layerShadowOpacity];
  2683. }
  2684. - (CGFloat)layerShadowOpacity {
  2685. CGFloat alpha = 0.0;
  2686. if (!self.layerShadowColor) return alpha;
  2687. [self.layerShadowColor getWhite:nil alpha:&alpha];
  2688. return alpha;
  2689. }
  2690. - (void)setButtonAtIndex:(NSUInteger)index enabled:(BOOL)enabled {
  2691. [self setButtonEnabled:enabled atIndex:index];
  2692. }
  2693. @end