简体   繁体   中英

UIImageView not replacing another view

I have this code to create my original UIView:

itemView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - CELL_HEIGHT - 10, 0, CELL_HEIGHT, CELL_HEIGHT)];
itemView.backgroundColor = [UIColor redColor];
[self addSubview:itemView];

And then I have this code later on:

UIImageView *accessory = [[UIImageView alloc] initWithFrame:cell.itemView.frame];
accessory.contentMode = UIViewContentModeCenter;
accessory.image = [UIImage imageNamed:@"icon_settings_fwd.png"];

itemView = accessory;

But all I'm seeing is red. It hasn't replaced it with the UIImage. Anybody know why?

itemView is just a pointer to the UIView instance with the red background color. When you add this to another view as a subview, the superview retains the newly added subview and thus from now on, maintains its own copy of its pointer. It doesn't know anything about you reusing the original pointer. You have to remove the first subview (the one with the red background color) first, then add the second view as a subview.

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