转载请注明出处。

升级到ios7后,旧项目中使用的继承UIAlertView的自定义alertview无法正常显示了,无奈只好换思路去实现,改成从当前keywindow下创建要显示的alertview,并模仿了系统alertview

.h文件

 
  1. #import <UIKit/UIKit.h>
  2.  
  3. typedef enum
  4. {
  5. CustomAlertViewType_Msg_TwoBtn=,//含有title,提示内容以及两个button.
  6. CustomAlertViewType_Msg_OneBtn,//含有title,提示内容以及一个button.
  7. CustomAlertViewType_ActivityIndiAndMsg_OneBtn, //含有title,UIActivityIndicatorView控件,提示内容以及一个button.
  8. CustomAlertViewType_Msg_TextField_TwoBtn,
  9. CustomAlertViewType_JalBreakBuy_Login,
  10. CustomAlertViewType_RemindTime,
  11.  
  12. }CustomAlertViewType;
  13.  
  14. @protocol CustomAlertViewDelegate;
  15.  
  16. @interface CustomAlertView : UIView<UITextFieldDelegate>
  17. {
  18. CustomAlertViewType _alertViewType;
  19. id<CustomAlertViewDelegate> _customDelegate;
  20.  
  21. UILabel* titleLabel;
  22. UILabel* contentLabel;
  23.  
  24. UIButton* leftBtn;
  25. UIButton* rightBtn;
  26. UIButton* centerBtn;
  27.  
  28. UIActivityIndicatorView *indicatorView;
  29.  
  30. UITextField* textField;
  31.  
  32. UIView* _alertView;
  33. UIView* _bgView;
  34.  
  35. }
  36. @property (nonatomic,assign) id<CustomAlertViewDelegate> customDelegate;
  37. @property (nonatomic,retain) UILabel* contentLabel;
  38. @property (nonatomic,assign) UITextField* textField;
  39.  
  40. //含有title,提示内容以及两个button.
  41. - (id)initWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate;
  42.  
  43. - (id)initWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate msgFontSize:(CGFloat)fontSize;
  44. //含有title,提示内容以及一个button.
  45. - (id)initWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle;
  46.  
  47. //含有title,UIActivityIndicatorView控件,提示内容以及一个button.
  48. - (id)initProgressAlertViewWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle delegate:(id<CustomAlertViewDelegate>) _delegate;
  49.  
  50. //含有title,textfield,提示内容以及两个button.
  51. - (id)initTextFieldWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate;
  52.  
  53. //含title,两个button,密码输入textfield,用户名等提示信息
  54. -(id)initLoginWithDelegate:(id<CustomAlertViewDelegate>)delegate userId:(NSString*)userid title:(NSString*)strTitle rightBtnTitle:(NSString*)strRbt;
  55.  
  56. - (id)initRemindAlert;
  57.  
  58. -(void) show;
  59. - (void) hideAlertView;
  60.  
  61. -(void) setTitle:(NSString*) title;
  62. @end
  63.  
  64. @protocol CustomAlertViewDelegate <NSObject>
  65.  
  66. @optional
  67.  
  68. - (void) leftBtnPressedWithinalertView:(CustomAlertView*)alert;
  69. - (void) rightBtnPressedWithinalertView:(CustomAlertView*)alert;
  70. - (void) centerBtnPressedWithinalertView:(CustomAlertView*)alert;
  71.  
  72. @end

.m文件

  1. #import "CustomAlertView.h"
  2. #import "UIScreen+Frame.h"
  3. #import "CustomAlertView.h"
  4.  
  5. #define MAX_CATEGORY_NAME_LENGTH 9
  6. #define kTagViewTextFieldJalBreakPassW (1001)
  7. @implementation CustomAlertView
  8.  
  9. @synthesize customDelegate = _customDelegate;
  10. @synthesize contentLabel;
  11. @synthesize textField;
  12.  
  13. //含有title,提示内容以及两个button.
  14. - (id)initWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate
  15. {
  16. if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
  17. {
  18. // Initialization code
  19. _alertViewType=CustomAlertViewType_Msg_TwoBtn;
  20. self.customDelegate=_delegate;
  21.  
  22. [self setBackgroundColor:[UIColor clearColor]];
  23. _bgView = [[UIView alloc] initWithFrame:self.frame];
  24. [_bgView setBackgroundColor:[UIColor blackColor]];
  25. [self addSubview:_bgView];
  26. [_bgView release];
  27.  
  28. CGRect alertRect = [self getAlertBounds];
  29. _alertView = [[UIView alloc] initWithFrame:alertRect];
  30.  
  31. UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
  32. alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
  33. [_alertView addSubview:alertBg];
  34. [alertBg release];
  35.  
  36. titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  37. titleLabel.textColor = [UIColor whiteColor];
  38. titleLabel.backgroundColor = [UIColor clearColor];
  39. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  40. titleLabel.text =title;
  41. titleLabel.textAlignment=UITextAlignmentCenter;
  42. [_alertView addSubview:titleLabel];
  43. [titleLabel release];
  44.  
  45. contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  46. contentLabel.textColor = [UIColor whiteColor];
  47. contentLabel.backgroundColor = [UIColor clearColor];
  48. contentLabel.font = [UIFont systemFontOfSize:15.0];
  49. contentLabel.text =msg;
  50. contentLabel.textAlignment=UITextAlignmentCenter;
  51. contentLabel.lineBreakMode = UILineBreakModeWordWrap;
  52. contentLabel.numberOfLines = ;
  53. [_alertView addSubview:contentLabel];
  54. [contentLabel release];
  55.  
  56. UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
  57. UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
  58. rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  59. [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
  60. [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
  61. rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
  62. [_alertView addSubview:rightBtn];
  63. [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  64.  
  65. leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  66. [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
  67. [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
  68. [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  69. leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
  70. [_alertView addSubview:leftBtn];
  71.  
  72. [self addSubview:_alertView];
  73. [_alertView release];
  74. [self showBackground];
  75. [self showAlertAnmation];
  76.  
  77. }
  78. return self;
  79. }
  80.  
  81. //可修改字体
  82. - (id)initWithTitle:(NSString*)title
  83. msg:(NSString*)msg
  84. rightBtnTitle:(NSString*)rightTitle
  85. leftBtnTitle:(NSString*)leftTitle
  86. delegate:(id<CustomAlertViewDelegate>) _delegate
  87. msgFontSize:(CGFloat)fontSize
  88. {
  89. if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
  90. {
  91. // Initialization code
  92. _alertViewType=CustomAlertViewType_Msg_TwoBtn;
  93. self.customDelegate=_delegate;
  94.  
  95. [self setBackgroundColor:[UIColor clearColor]];
  96. _bgView = [[UIView alloc] initWithFrame:self.frame];
  97. [_bgView setBackgroundColor:[UIColor blackColor]];
  98. [self addSubview:_bgView];
  99. [_bgView release];
  100.  
  101. CGRect alertRect = [self getAlertBounds];
  102. _alertView = [[UIView alloc] initWithFrame:alertRect];
  103.  
  104. UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
  105. alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
  106. [_alertView addSubview:alertBg];
  107. [alertBg release];
  108.  
  109. titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  110. titleLabel.textColor = [UIColor whiteColor];
  111. titleLabel.backgroundColor = [UIColor clearColor];
  112. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  113. titleLabel.text =title;
  114. titleLabel.textAlignment=UITextAlignmentCenter;
  115. [_alertView addSubview:titleLabel];
  116. [titleLabel release];
  117.  
  118. contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  119. contentLabel.textColor = [UIColor whiteColor];
  120. contentLabel.backgroundColor = [UIColor clearColor];
  121. contentLabel.font = [UIFont systemFontOfSize:fontSize];
  122. contentLabel.text =msg;
  123. contentLabel.textAlignment=UITextAlignmentCenter;
  124. contentLabel.lineBreakMode = UILineBreakModeWordWrap;
  125. contentLabel.numberOfLines = ;
  126. [_alertView addSubview:contentLabel];
  127. [contentLabel release];
  128.  
  129. UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
  130. UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
  131. rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  132. [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
  133. [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
  134. rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
  135. [_alertView addSubview:rightBtn];
  136. [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  137.  
  138. leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  139. [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
  140. [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
  141. [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  142. leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
  143. [_alertView addSubview:leftBtn];
  144.  
  145. [self addSubview:_alertView];
  146. [_alertView release];
  147. [self showBackground];
  148. [self showAlertAnmation];
  149. }
  150. return self;
  151. }
  152.  
  153. - (id)initWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle
  154. {
  155.  
  156. self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]];
  157. if(self)
  158. {
  159. _alertViewType=CustomAlertViewType_Msg_OneBtn;
  160. [self setBackgroundColor:[UIColor clearColor]];
  161. _bgView = [[UIView alloc] initWithFrame:self.frame];
  162. [_bgView setBackgroundColor:[UIColor blackColor]];
  163. [self addSubview:_bgView];
  164. [_bgView release];
  165.  
  166. CGRect alertRect = [self getAlertBounds];
  167. _alertView = [[UIView alloc] initWithFrame:alertRect];
  168.  
  169. UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
  170. alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
  171. [_alertView addSubview:alertBg];
  172. [alertBg release];
  173.  
  174. titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  175. titleLabel.textColor = [UIColor whiteColor];
  176. titleLabel.backgroundColor = [UIColor clearColor];
  177. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  178. titleLabel.text =title;
  179. titleLabel.textAlignment=UITextAlignmentCenter;
  180. [_alertView addSubview:titleLabel];
  181. [titleLabel release];
  182.  
  183. contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  184. contentLabel.textColor = [UIColor whiteColor];
  185. contentLabel.backgroundColor = [UIColor clearColor];
  186. contentLabel.font = [UIFont systemFontOfSize:15.0];
  187. contentLabel.text =msg;
  188. contentLabel.textAlignment=UITextAlignmentCenter;
  189. [_alertView addSubview:contentLabel];
  190. [contentLabel release];
  191.  
  192. UIImage* selectedImg=[UIImage imageNamed:@"bigbuttonbkimg.png"];
  193. centerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  194. [centerBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
  195. [centerBtn setTitle:centerTitle forState:UIControlStateNormal];
  196. centerBtn.frame=CGRectMake(, , , );
  197. [_alertView addSubview:centerBtn];
  198. [centerBtn addTarget:self action:@selector(centerBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  199.  
  200. [self addSubview:_alertView];
  201. [_alertView release];
  202. [self showBackground];
  203. [self showAlertAnmation];
  204. }
  205. return self;
  206. }
  207.  
  208. //含有title,UIActivityIndicatorView控件,提示内容以及一个button.
  209. - (id)initProgressAlertViewWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle delegate:(id<CustomAlertViewDelegate>) _delegate
  210. {
  211. if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
  212. {
  213. // Initialization code
  214. _alertViewType=CustomAlertViewType_ActivityIndiAndMsg_OneBtn;
  215. self.customDelegate=_delegate;
  216.  
  217. [self setBackgroundColor:[UIColor clearColor]];
  218. _bgView = [[UIView alloc] initWithFrame:self.frame];
  219. [_bgView setBackgroundColor:[UIColor blackColor]];
  220. [self addSubview:_bgView];
  221. [_bgView release];
  222.  
  223. CGRect alertRect = [self getAlertBounds];
  224. _alertView = [[UIView alloc] initWithFrame:alertRect];
  225.  
  226. UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
  227. alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
  228. [_alertView addSubview:alertBg];
  229. [alertBg release];
  230.  
  231. titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  232. titleLabel.textColor = [UIColor whiteColor];
  233. titleLabel.backgroundColor = [UIColor clearColor];
  234. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  235. titleLabel.text =title;
  236. titleLabel.textAlignment=UITextAlignmentCenter;
  237. [_alertView addSubview:titleLabel];
  238. [titleLabel release];
  239.  
  240. indicatorView= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(80.0, 45.0, 30.0, 30.0)];
  241. indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  242. indicatorView.hidesWhenStopped=NO;
  243. [_alertView addSubview:indicatorView];
  244. [indicatorView release];
  245. [indicatorView startAnimating];
  246.  
  247. contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(120.0, 50.0, 150.0, 20.0)];
  248. contentLabel.textColor = [UIColor whiteColor];
  249. contentLabel.backgroundColor = [UIColor clearColor];
  250. contentLabel.font = [UIFont boldSystemFontOfSize:15.0];
  251. contentLabel.text =msg;
  252. contentLabel.textAlignment=UITextAlignmentLeft;
  253. [_alertView addSubview:contentLabel];
  254. [contentLabel release];
  255.  
  256. UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
  257. centerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  258. [centerBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
  259. [centerBtn setTitle:centerTitle forState:UIControlStateNormal];
  260. centerBtn.frame=CGRectMake(, , , );
  261. [_alertView addSubview:centerBtn];
  262. [centerBtn addTarget:self action:@selector(centerBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  263.  
  264. [self addSubview:_alertView];
  265. [_alertView release];
  266. [self showBackground];
  267. [self showAlertAnmation];
  268. }
  269. return self;
  270. }
  271.  
  272. //含有title,textfield,提示内容以及两个button.
  273. - (id)initTextFieldWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate
  274. {
  275. if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
  276. {
  277. // Initialization code
  278. _alertViewType=CustomAlertViewType_Msg_TextField_TwoBtn;
  279. self.customDelegate=_delegate;
  280.  
  281. [self setBackgroundColor:[UIColor clearColor]];
  282. _bgView = [[UIView alloc] initWithFrame:self.frame];
  283. [_bgView setBackgroundColor:[UIColor blackColor]];
  284. [self addSubview:_bgView];
  285. [_bgView release];
  286.  
  287. CGRect alertRect = [self getAlertBounds];
  288. _alertView = [[UIView alloc] initWithFrame:alertRect];
  289.  
  290. UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
  291. alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
  292. [_alertView addSubview:alertBg];
  293. [alertBg release];
  294.  
  295. titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  296. titleLabel.textColor = [UIColor whiteColor];
  297. titleLabel.backgroundColor = [UIColor clearColor];
  298. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  299. titleLabel.text =title;
  300. titleLabel.textAlignment=UITextAlignmentCenter;
  301. [_alertView addSubview:titleLabel];
  302. [titleLabel release];
  303.  
  304. contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 33.0, 300.0, 12.0)];
  305. contentLabel.textColor = [UIColor clearColor];
  306. contentLabel.backgroundColor = [UIColor clearColor];
  307. contentLabel.font = [UIFont boldSystemFontOfSize:8.0];
  308. contentLabel.textAlignment=UITextAlignmentCenter;
  309. [_alertView addSubview:contentLabel];
  310. [contentLabel release];
  311.  
  312. textField = [[[UITextField alloc] initWithFrame:CGRectMake(, , , )] autorelease];
  313. textField.borderStyle = UITextBorderStyleRoundedRect;
  314. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  315. textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  316. textField.placeholder = msg;
  317. [textField addTarget:self action:@selector(textFieldChanged) forControlEvents:UIControlEventEditingChanged];
  318. [_alertView addSubview:textField];
  319. [textField release];
  320.  
  321. UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
  322. UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
  323. rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  324. [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
  325. [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
  326. rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
  327. [_alertView addSubview:rightBtn];
  328. [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  329.  
  330. leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  331. [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
  332. [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
  333. [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  334. leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
  335. [_alertView addSubview:leftBtn];
  336.  
  337. [self addSubview:_alertView];
  338. [_alertView release];
  339. [self showBackground];
  340. [self showAlertAnmation];
  341. }
  342. return self;
  343. }
  344.  
  345. -(id)initLoginWithDelegate:(id<CustomAlertViewDelegate>)delegate userId:(NSString*)userid title:(NSString*)strTitle rightBtnTitle:(NSString*)strRbt
  346. {
  347. if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
  348. {
  349.  
  350. _alertViewType = CustomAlertViewType_JalBreakBuy_Login;
  351. self.customDelegate = delegate;
  352.  
  353. [self setBackgroundColor:[UIColor clearColor]];
  354. _bgView = [[UIView alloc] initWithFrame:self.frame];
  355. [_bgView setBackgroundColor:[UIColor blackColor]];
  356. [self addSubview:_bgView];
  357. [_bgView release];
  358.  
  359. CGRect alertRect = [self getAlertBounds];
  360. _alertView = [[UIView alloc] initWithFrame:alertRect];
  361.  
  362. UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
  363. alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
  364. [_alertView addSubview:alertBg];
  365. [alertBg release];
  366.  
  367. titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
  368. titleLabel.textColor = [UIColor whiteColor];
  369. titleLabel.backgroundColor = [UIColor clearColor];
  370. titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
  371. titleLabel.text = strTitle;
  372. titleLabel.textAlignment=UITextAlignmentCenter;
  373. [_alertView addSubview:titleLabel];
  374. [titleLabel release];
  375.  
  376. CGFloat xLabel1 = ;
  377. CGFloat xLabel2 = ;
  378. CGFloat yLevel1 = ;
  379. CGFloat yLevel2 = ;
  380.  
  381. UILabel* label = nil;
  382. label = [[UILabel alloc]initWithFrame:CGRectMake(xLabel1, yLevel1, , )];
  383. label.backgroundColor = [UIColor clearColor];
  384. label.text = @"账号:";
  385. label.textColor = [UIColor whiteColor];
  386. label.font = [UIFont boldSystemFontOfSize:17.0];
  387. label.textAlignment = UITextAlignmentCenter;
  388. [_alertView addSubview:label];
  389. [label release];
  390.  
  391. label = [[UILabel alloc]initWithFrame:CGRectMake(xLabel2, yLevel1,, )];
  392. label.backgroundColor = [UIColor clearColor];
  393. label.text = userid;
  394. label.textColor = [UIColor whiteColor];
  395. label.font = [UIFont boldSystemFontOfSize:17.0];
  396. label.textAlignment = UITextAlignmentLeft;
  397. [_alertView addSubview:label];
  398. [label release];
  399.  
  400. label = [[UILabel alloc]initWithFrame:CGRectMake(xLabel1, yLevel2, , )];
  401. label.backgroundColor = [UIColor clearColor];
  402. label.text = @"密码:";
  403. label.textColor = [UIColor whiteColor];
  404. label.font = [UIFont boldSystemFontOfSize:17.0];
  405. label.textAlignment = UITextAlignmentCenter;
  406. [_alertView addSubview:label];
  407. [label release];
  408.  
  409. textField = [[[UITextField alloc]initWithFrame:CGRectMake(xLabel2, yLevel2, , )] autorelease];
  410. textField.delegate = self;
  411. textField.textColor = kColorLoginInput;
  412. textField.tag= kTagViewTextFieldJalBreakPassW;
  413. textField.borderStyle = UITextBorderStyleRoundedRect;
  414. textField.secureTextEntry = YES;
  415. textField.returnKeyType = UIReturnKeyDone;
  416. textField.autocorrectionType = UITextAutocorrectionTypeNo;
  417. textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  418. textField.font = [UIFont systemFontOfSize:];
  419. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  420. textField.leftViewMode = UITextFieldViewModeAlways;
  421. textField.keyboardType = UIKeyboardTypeASCIICapable ;
  422. [_alertView addSubview:textField];
  423. [textField release];
  424.  
  425. UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
  426. UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
  427.  
  428. rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  429. [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
  430. [rightBtn setTitle:strRbt forState:UIControlStateNormal];
  431. rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
  432. [_alertView addSubview:rightBtn];
  433. [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  434.  
  435. leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  436. [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
  437. [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
  438. [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
  439. leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
  440. [_alertView addSubview:leftBtn];
  441.  
  442. [self addSubview:_alertView];
  443. [_alertView release];
  444. [self showBackground];
  445. [self showAlertAnmation];
  446.  
  447. }
  448.  
  449. return self;
  450.  
  451. }
  452.  
  453. -(void) show
  454. {
  455. UIWindow* window = [[UIApplication sharedApplication] keyWindow];
  456. NSArray* windowViews = [window subviews];
  457. if(windowViews && [windowViews count]>){
  458. UIView* subView = [windowViews objectAtIndex:[windowViews count]-];
  459. for(UIView* aSubView in subView.subviews)
  460. {
  461. [aSubView.layer removeAllAnimations];
  462.  
  463. }
  464. [subView addSubview:self];
  465. }
  466.  
  467. }
  468.  
  469. - (void)showBackground
  470. {
  471. _bgView.alpha = ;
  472. [UIView beginAnimations:@"fadeIn" context:nil];
  473. [UIView setAnimationDuration:0.35];
  474. _bgView.alpha = 0.6;
  475. [UIView commitAnimations];
  476. }
  477.  
  478. -(void) showAlertAnmation
  479. {
  480. CAKeyframeAnimation * animation;
  481. animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  482. animation.duration = 0.30;
  483. animation.removedOnCompletion = YES;
  484. animation.fillMode = kCAFillModeForwards;
  485. NSMutableArray *values = [NSMutableArray array];
  486. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
  487. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
  488. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
  489. animation.values = values;
  490. [_alertView.layer addAnimation:animation forKey:nil];
  491.  
  492. }
  493.  
  494. -(void) hideAlertAnmation
  495. {
  496. [UIView beginAnimations:@"fadeIn" context:nil];
  497. [UIView setAnimationDuration:0.35];
  498. _bgView.alpha = 0.0;
  499. [UIView commitAnimations];
  500. }
  501.  
  502. -(CGRect)getAlertBounds
  503. {
  504. CGRect retRect;
  505.  
  506. if (_alertViewType == CustomAlertViewType_JalBreakBuy_Login)
  507. {
  508.  
  509. retRect= CGRectMake((self.frame.size.width-)/, (self.frame.size.height-)/, , );
  510.  
  511. }
  512. else
  513. {
  514. UIImage* image=[UIImage imageNamed:@"AlertView_background.png"];
  515. CGSize imageSize = image.size;
  516. retRect= CGRectMake((self.frame.size.width-imageSize.width)/, (self.frame.size.height-imageSize.height)/, imageSize.width, imageSize.height);
  517.  
  518. }
  519.  
  520. return retRect;
  521. }
  522.  
  523. - (void) hideAlertView
  524. {
  525. _alertView.hidden = YES;
  526. [self hideAlertAnmation];
  527. [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
  528. }
  529.  
  530. -(void) removeFromSuperview
  531. {
  532. [super removeFromSuperview];
  533. }
  534.  
  535. - (void) leftBtnPressed:(id)sender
  536. {
  537. if (_customDelegate && [_customDelegate respondsToSelector:@selector(leftBtnPressedWithinalertView:)])
  538. {
  539. [_customDelegate leftBtnPressedWithinalertView:self];
  540. }
  541. else
  542. {
  543. [self hideAlertView];
  544. }
  545. }
  546.  
  547. - (void) rightBtnPressed:(id)sender
  548. {
  549. if (_customDelegate && [_customDelegate respondsToSelector:@selector(rightBtnPressedWithinalertView:)])
  550. {
  551. [_customDelegate rightBtnPressedWithinalertView:self];
  552. }
  553. else
  554. {
  555. [self hideAlertView];
  556. }
  557. }
  558.  
  559. - (void) centerBtnPressed:(id)sender
  560. {
  561. if (_customDelegate && [_customDelegate respondsToSelector:@selector(centerBtnPressedWithinalertView:)])
  562. {
  563. [_customDelegate centerBtnPressedWithinalertView:self];
  564. }
  565. else
  566. {
  567. [self hideAlertView];
  568. }
  569. }
  570.  
  571. -(void) setTitle:(NSString*) title
  572. {
  573. titleLabel.text = title;
  574. }
  575.  
  576. -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  577. {
  578. [self endEditing:YES];
  579. }
  580.  
  581. -(void) textFieldChanged
  582. {
  583. if ([textField.text length] > MAX_CATEGORY_NAME_LENGTH)
  584. {
  585. textField.text = [textField.text substringToIndex:MAX_CATEGORY_NAME_LENGTH];
  586. }
  587. }
  588.  
  589. #pragma mark - DelegateTextField
  590.  
  591. - (BOOL)textFieldShouldReturn:(UITextField *)_textField
  592. {
  593. if (_textField.tag == kTagViewTextFieldJalBreakPassW)
  594. {
  595. [self rightBtnPressed:nil];
  596. return NO;
  597. }
  598.  
  599. return YES;
  600. }
  601.  
  602. - (BOOL)textField:(UITextField *)textField_ shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  603. {
  604. if (textField_.tag == kTagViewTextFieldJalBreakPassW)
  605. {
  606.  
  607. if (string && [string length] && [textField_.text length]>)
  608. {
  609. return NO;
  610. }
  611.  
  612. }
  613.  
  614. return YES;
  615.  
  616. }
  617.  
  618. @end

调用:

  1. CustomAlertView* alert = [[CustomAlertView alloc]initWithTitle:nil
  2. msg:@"测试alertview"
  3. centerBtnTitle:@"确定"];
  4. [alert show];
  5. [alert release];
  6.   

兼容iOs7的自定义alertView的更多相关文章

  1. iOS 读取相册二维码,兼容ios7(使用CIDetector 和 ZXingObjC)

    ios从相册读取二维码,在ios8以上,苹果提供了自带的识别图片二维码的功能,这种方式效率最好,也是最推荐的,但是如果你的系统需要向下兼容ios7,就必须用其他方式. 这里我选择的是 ZXingObj ...

  2. iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析

    做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...

  3. Swift3.0生成二维码、扫描二维码、相册读取二维码,兼容iOS7(结合ZXingObjC)

    二维码生成 //MARK: 传进去字符串,生成二维码图片(>=iOS7) text:要生成的二维码内容 WH:二维码高宽 private func creatQRCodeImage(text: ...

  4. 自定义AlertView的方法和改变Alert的弹出位置以及其宽度

    此方法在IOS7中不适合 一.自定义AlertView 1.首先新建一个OC类继承与AlertView. 2.然后再.m中添加方法 - (void)layoutSubviews 可以再这个方法里边改变 ...

  5. Swift自定义AlertView

    今天项目加新需求,添加积分过期提醒功能: 第一反应就用系统的UIAlertViewController,但是message中积分是需要红色显示. // let str = "尊敬的顾客,您有 ...

  6. [iOS开发]Xcode8兼容iOS7以及低版本Xcode调试高版本iOS系统

    现在的项目一般都要兼容iOS7系统,同时也要兼容iOS10,在Xcode8上面,默认情况下无法调试iOS7,因为缺乏调试iOS7需要的配置文件.同时在低版本的Xcode上面(8以下),也无法调试iOS ...

  7. Xcode8兼容iOS7以及低版本Xcode调试高版本iOS系统

    我们使用Xcode8新建的工程,默认支持的最低系统是iOS8,我们可以手动更改版本到7.0,但是不支持真机调试. 现在的项目一般都要兼容iOS7系统,同时也要兼容iOS10,在Xcode8上面,默认情 ...

  8. iOS开发——自定义AlertView

    自定义的AlertView,可以选择出现的动画方式,正文信息高度自动变化,特意做了几个可以对比.没啥难点,直接上代码,一看就懂. 1.在YYTAlertView.h文件中 // //  YYTAler ...

  9. iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题

    前言: 本文由DevDiv社区@Vincent 原创,转载请注明出处! http://www.devdiv.com/iOS_iPhone-ios_ios_uibarbuttonitem_-thread ...

随机推荐

  1. JVM 内存调优 与 实际案例

    堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...

  2. Python学习札记(十九) 高级特性5 迭代器

    参考:迭代器 Note 1.可用于for循环的对象有两类:(1)集合数据类型:list tuple dict str set (2)Generator:生成器和含yield语句的函数.这些可以直接作用 ...

  3. gTest&gMock learning

    在C++中,编写服务后的一种测试方式是使用google的gTest和gMock结合 之前写py,测试方式是将服务挂起,使用工具模拟请求发包,check resp,这样的缺点在于不方便,即使存下了所有的 ...

  4. [PostgreSql]PostgreSql创建函数及函数的执行

    1.准备好创建函数的脚本 -- FUNCTION: public.dynamic_placelist_select(integer, timestamp without time zone) -- D ...

  5. Les13 性能管理

    目标 使用Oracle Enterprise Manager监视性能 使用自动内存管理(AMM) 使用内存指导调整内存缓冲区的大小 查看与性能相关的动态视图 排除无效和不可用对象产生的故障 性能监视 ...

  6. 《OpenCL编程指南》之 与Direct3D互操作

    介绍OpenCL与D3D 10之间的互操作. 1.初始化OpenCL上下文实现Direct3D互操作 OpenCL共享由pragma cl_khr_d3d10_sharing启用: #pragma O ...

  7. 【zznu-2060】 Minsum Plus(最小正子段和)

    题目描述 题意简单到令人发指! 序列A由N个整数组成,从中选出一个连续的子序列,使得这个子序列的和为正数,且和为所有和大于零的子序列中的最小值. 将这个值输出,若无解,输出no solution. 输 ...

  8. hdu1151

    题解: 二分图边覆盖 n-最大匹配 代码: #include<cstdio> #include<cmath> #include<algorithm> #includ ...

  9. jqeury 基础

    jquery 选择器: 基本选择器:#id ..class.*(匹配所有) 层次选择器: $(div span) 选取<div>里的所有的<span>元素. $(div> ...

  10. Hadoop学习资料整理

    1.hadoop相关 hadoop 0.18文档(详细介绍Hadoop,MapReduce,FS Shell,Streaming等) hadoop资料汇总 2.实习的时候用的是streaming,非j ...