addSubview doesn't work
我是Objective-C的初学者。当我在方法设置视图中使用AddSubView查看图像时,这不起作用。
1 2 3 4 5 6 7 8 9 10 | h. @interface CollectionViewController : UICollectionViewController @end @interface MyCell : UICollectionViewCell @end |
和
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 | m. @interface MyCell () @end @implementation MyCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) [self setupView]; return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) [self setupView]; return self; } -(UIImageView *)myImageView { UIImageView *imageView = [UIImageView new]; imageView.image = [UIImage imageNamed:@"photo.jpg"]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.layer.masksToBounds = true; return imageView; } -(void)setupView { self.backgroundColor = [UIColor whiteColor]; [self addSubview:self.myImageView]; } @end |
有很多潜在的问题,但很可能图像视图的框架不正确。尝试用创建它
1 2 | UIImage *image = [UIImage imageNamed:@"photo.jpg"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; |