简体   繁体   中英

iOS: How to dynamically add image to a parent view?

I've got a main image on the screen, and have an "overlay" UIView placed on top of it with the background color set to Clear Color. I want to dynamically place other images inside the overlay view as follows:

UIImage* m_icon = [[UIImage alloc]initWithContentsOfFile:@"icon.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:m_icon];
[m_OverlayView addSubview:imageView];

But the icon image isn't showing up. What am I missing?

You may try:

NSString *icon_path = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"];
UIImage* m_icon = [[UIImage alloc]initWithContentsOfFile:icon_path]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:m_icon];
[m_OverlayView addSubview:imageView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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