修改UIPickView的高度

发现了一个问题,UIPickerView不能改变小于216的高度,利用setframe方法无法修改UIPickerView的高度。


解决方法:

   1、加上pickerView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 后,能变成超过216的高度,但是依旧不能减小

   2.可以通过创建UIPickerView子类,覆盖- (void) setFrame:(CGRect)frame方法和- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view方法,通过CGAffineTransformTranslate仿射变换解决。


  代码如下:

        

#define FIXED_PICKER_HEIGHT 216.0f
- ( void ) setFrame:(CGRect)frame
{
     CGFloat targetHeight = frame.size.height;
     CGFloat scaleFactor = targetHeight / FIXED_PICKER_HEIGHT;
     frame.size.height = FIXED_PICKER_HEIGHT; //fake normal conditions for super
     self .transform = CGAffineTransformIdentity; //fake normal conditions for super
     [ super   setFrame:frame];
     frame.size.height = targetHeight;
     CGFloat dX= self .bounds.size.width/2, dY= self .bounds.size.height/2;
     self .transform = CGAffineTransformTranslate(CGAffineTransformScale(CGAffineTransformMakeTranslation(-dX, -dY), 1, scaleFactor), dX, dY);
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:(UIView *)view
{
     //Your code goes here
 
     CGFloat inverseScaleFactor = FIXED_PICKER_HEIGHT/ self .frame.size.height;
     CGAffineTransform scale = CGAffineTransformMakeScale(1, inverseScaleFactor);
     view.transform = scale;
     return   view;
}





你可能感兴趣的:(ios开发技巧)