简体   繁体   中英

Changing the colour of UIView that is created dynamically

I am creating a UIView and its subviews (such as UITextfield, UIButton etc.) dynamically( by reading from JSON )

I am giving tags to all UIView and try to call by tag.

For instance I am trying to change the background colour of a view inside another view.

parent view's tag is 1, subview's is 11.

My sample code is as follows;

UIView *temp = [self.view viewWithTag:1];

if([[temp viewWithTag:11] isKindOfClass:[UIView class]])
{
    [((UIView*)[temp viewWithTag:11]) setBackgroundColor:[UIColor blackColor]];
}

But there is no change, no effect.

Could you please help me on solving this.

#define PARENT_VIEW_TAG 1
#define CHILD_VIEW_TAG 11

    UIView *parentView = [self.view viewWithTag:PARENT_VIEW_TAG];

    for (UIView *vw in [parentView subviews]) {

        if(vw.tag == CHILD_VIEW_TAG)
        {
            [vw setBackgroundColor:[UIColor blackColor]];
        }
    }

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