簡體   English   中英

addSubview 不起作用

[英]addSubview doesn't work

我是Objective-C的初學者。 當我在 setupView 方法中使用 addSubview 來查看圖像時,這不起作用。

h.


    @interface CollectionViewController : UICollectionViewController 

    @end

    @interface MyCell : UICollectionViewCell 

    @end

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

在此處輸入圖片說明

有很多潛在的問題,但很可能是 imageView 的框架不正確。 嘗試創建它

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