iOS7 UIPickerView 无法正确显示带有图像的自定义视图 [英] iOS7 UIPickerView doesn't properly display custom views with images

查看:28
本文介绍了iOS7 UIPickerView 无法正确显示带有图像的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题开始在 iOS7 中使用新的 UIPickerView 控制器发生.要在 UIPickerView 控制器中使用图像,您必须使用委托方法返回图像:

This issue started happening in iOS7 with the new UIPickerView controller. To use images in your UIPickerView controller you must use the delegate method to return an image:

pickerView:viewForRow:forComponent:reusingView:

pickerView:viewForRow:forComponent:reusingView:

问题是屏幕随后会出现各种奇怪的行为 - 当您在控件上下移动手指时,图像视图会消失.

The problem is that the screen subsequently exhibits all kinds of strange behavior - the image views disappear as you move your finger up and down the control.

推荐答案

这是发布在开发论坛上的解决方案,适用于 iOS 7.0.2:

This is a solution posted on a dev forum which works as of iOS 7.0.2:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    // self.myImages is an array of UIImageView objects
    UIView * myView = [self.myImages objectAtIndex:row];

    // first convert to a UIImage
    UIGraphicsBeginImageContextWithOptions(myView.bounds.size, NO, 0);

    [myView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // then convert back to a UIImageView and return it
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    return imageView;
}

这篇关于iOS7 UIPickerView 无法正确显示带有图像的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆