在UIActionSheet中嵌入UIPickerView,美观漂亮。我自己的项目中也是这么用的,在IOS7.1之前都运行的非常完美,但是当系统升级到IOS8.1之后,运行出来的效果就非常的糟糕,ActionSheet不能够完全的显示出来内容。
下面是IOS8.1中运行的效果
为什么UIActionSheet里面嵌入UIPickerView会不兼容IOS8.1呢
那么在UIAlertController中嵌入UIPickerView可以解决问题吗?
那么到底怎么解决这个问题呢?
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
#import "SimulateActionSheet.h" #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)] @interface SimulateActionSheet(){ UIColor *toolBarColor; UIColor *textColorNormal; UIColor *textColorPressed; UIColor *pickerBgColor; } @end @implementation SimulateActionSheet +(instancetype)styleDefault{ SimulateActionSheet* sheet = [[SimulateActionSheet alloc]initWithFrame:CGRectMake( 0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)]; [sheet setBackgroundColor:[UIColor clearColor]]; sheet.toolBar = [sheet actionToolBar]; sheet.pickerView = [sheet actionPicker]; [sheet addSubview:sheet.toolBar]; [sheet addSubview:sheet.pickerView]; return sheet; } -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self != nil) { toolBarColor = RGBACOLOR(240.0, 240.0, 240.0, 0.9); textColorNormal = RGBACOLOR(0, 146.0, 255.0, 1); textColorPressed = RGBACOLOR(209.0, 213.0, 219.0, 0.9); pickerBgColor = textColorPressed; } return self; } -(void)setupInitPostion:(UIViewController *)controller{ [UIApplication.sharedApplication.keyWindow?UIApplication.sharedApplication.keyWindow:UIApplication.sharedApplication.windows[0] addSubview:self]; [self.superview bringSubviewToFront:self]; CGFloat pickerViewYpositionHidden = UIScreen.mainScreen.bounds.size.height; [self.pickerView setFrame:CGRectMake(self.pickerView.frame.origin.x, pickerViewYpositionHidden, self.pickerView.frame.size.width, self.pickerView.frame.size.height)]; [self.toolBar setFrame:CGRectMake(self.toolBar.frame.origin.x, pickerViewYpositionHidden, self.toolBar.frame.size.width, self.toolBar.frame.size.height)]; } -(void)show:(UIViewController *)controller{ [self setupInitPostion:controller]; CGFloat toolBarYposition = UIScreen.mainScreen.bounds.size.height - (self.pickerView.frame.size.height + self.toolBar.frame.size.height); [UIView animateWithDuration:0.25f animations:^{ [self setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]]; [controller.view setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed]; [controller.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed]; [self.toolBar setFrame:CGRectMake(self.toolBar.frame.origin.x, toolBarYposition, self.toolBar.frame.size.width, self.toolBar.frame.size.height)]; [self.pickerView setFrame:CGRectMake(self.pickerView.frame.origin.x, toolBarYposition+self.toolBar.frame.size.height, self.pickerView.frame.size.width, self.pickerView.frame.size.height)]; } completion:nil]; } -(void)dismiss:(UIViewController *)controller{ [UIView animateWithDuration:0.25f animations:^{ [self setBackgroundColor:[UIColor clearColor]]; [controller.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal]; [controller.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeNormal]; [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIView* v = (UIView*)obj; [v setFrame:CGRectMake(v.frame.origin.x, UIScreen.mainScreen.bounds.size.height, v.frame.size.width, v.frame.size.height)]; }]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } -(UIView *)actionToolBar{ UIView *tools = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; tools.backgroundColor = toolBarColor; UIButton *cancle = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)]; [cancle setTitle:@"取消" forState:UIControlStateNormal]; [cancle setTitleColor:textColorPressed forState:UIControlStateHighlighted]; [cancle setTitleColor:textColorNormal forState:UIControlStateNormal]; [cancle addTarget:self action:@selector(actionCancle) forControlEvents:UIControlEventTouchUpInside]; [cancle sizeToFit]; [tools addSubview:cancle]; cancle.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *cancleConstraintLeft = [NSLayoutConstraint constraintWithItem:cancle attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]; NSLayoutConstraint *cancleConstrainY = [NSLayoutConstraint constraintWithItem:cancle attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0]; [tools addConstraint:cancleConstraintLeft]; [tools addConstraint:cancleConstrainY]; UIButton *ok = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)]; [ok setTitle:@"确定" forState:UIControlStateNormal]; [ok setTitleColor:textColorNormal forState:UIControlStateNormal]; [ok setTitleColor:textColorPressed forState:UIControlStateHighlighted]; [ok addTarget:self action:@selector(actionDone) forControlEvents:UIControlEventTouchUpInside]; [ok sizeToFit]; [tools addSubview:ok]; ok.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *okConstraintRight = [NSLayoutConstraint constraintWithItem:ok attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]; NSLayoutConstraint *okConstraintY = [NSLayoutConstraint constraintWithItem:ok attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0]; [tools addConstraint:okConstraintRight]; [tools addConstraint:okConstraintY]; return tools; } -(UIPickerView *)actionPicker;{ UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)]; picker.showsSelectionIndicator=YES; [picker setBackgroundColor:pickerBgColor]; return picker; } -(void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)anime{ [_pickerView selectRow:row inComponent:component animated:anime]; } -(NSInteger)selectedRowInComponent:(NSInteger)component{ return [_pickerView selectedRowInComponent:component]; } -(void)actionDone{ if([_delegate respondsToSelector:@selector(actionDone)]){ [_delegate actionDone]; } } -(void)actionCancle{ if ([_delegate respondsToSelector:@selector(actionCancle)]) { [_delegate actionCancle]; } } -(void)setDelegate:(id<SimulateActionSheetDelegate>)delegate{ _delegate = delegate; _pickerView.delegate = delegate; } @end |
demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#import "ViewController.h" #import "SimulateActionSheet.h" @interface ViewController ()<SimulateActionSheetDelegate, UIPickerViewDataSource>{ SimulateActionSheet *sheet; NSArray* arrays; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. arrays = [NSArray arrayWithObjects:@"letv",@"kankan",@"tencent",@"sohu",@"pptv", nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)action:(id)sender { sheet = [SimulateActionSheet styleDefault]; sheet.delegate = self; //必须在设置delegate之后调用,否则无法选中指定的行 [sheet selectRow:arrays.count/2 inComponent:0 animated:YES]; [sheet show:self]; } -(void)actionCancle{ [sheet dismiss:self]; } -(void)actionDone{ [sheet dismiss:self]; NSUInteger index = [sheet selectedRowInComponent:0]; NSLog(@"done with index of %d",index); } -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return arrays.count; } -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [arrays objectAtIndex:row]; } @end |
总结:问题就这样通过最原始的方式解决了,当然从复杂度来讲比单纯的UIActionView中嵌入UIPickerView高,但是兼容性得到了保障。哪位大神如果能够通过UIALertController来实现这个效果请一定分享给大家,如果需要项目源代码可以留下你的邮箱。
特意注册了个号来,谢谢lz!
https://github.com/mooncong/SimulateActionSheet.git
解决燃眉之急,急求源码,谢谢!
源码地址,https://github.com/mooncong/SimulateActionSheet
求源代码