简体   繁体   English

addSubview 不起作用

[英]addSubview doesn't work

I'm beginner in Objective-C.我是Objective-C的初学者。 When I use addSubview in method setupView to see image, this doesn't work.当我在 setupView 方法中使用 addSubview 来查看图像时,这不起作用。

h.


    @interface CollectionViewController : UICollectionViewController 

    @end

    @interface MyCell : UICollectionViewCell 

    @end

and

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

在此处输入图片说明

There are a lot of potential issues, but most likely the frame of the imageView isn't correct.有很多潜在的问题,但很可能是 imageView 的框架不正确。 Try creating it with尝试创建它

UIImage *image = [UIImage imageNamed:@"photo.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM