Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide/show the UIimageview?

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGRect viewRect = CGRectMake(250, 100, 30, 30);
    as = [[UIImageView alloc] initWithFrame:viewRect];
    as.backgroundColor=[UIColor clearColor];
    UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"check" ofType:@"png"]];
    [as setImage:img];
    [self.view addSubview:as];
    BOOL test= [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
    NSLog(@"%@", (test ? @"YES" : @"NO"));
    if(test == YES)
        {
            as.hidden=NO;
        }
    else
        {
            as.hidden=YES;
        }
}

The test results YES but the imageView doesn't listen the command .hidden or update every time when the viewDidAppear.If it is not when I restart the app and it disappear after I turn it to yes I show perfectly but after than I never goes always there I can't make it hidden. any idea why it is not reacting?

like image 247
john doe Avatar asked Aug 25 '11 10:08

john doe


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

What is use of UIImageView?

The class provides controls to set the duration and frequency when animating multiple images. It also allows you to stop and start the animation. All images associated with a UIImageView object should use the same scale.

How do I change aspect ratio in UIImageView?

If you are resizing UIImageView manually, set the content mode to UIViewContentModeScaleAspectFill . If you want to keep content mode UIViewContentModeScaleAspectFit do not resize imageview to 313. Image will adjust maximum possible width and height , keeping it's aspect ratio.


1 Answers

The problem is you create new UIImageView every time when your view appears. You have to create UIImageView as once:

- (void)loadView {
    [super loadView];
    CGRect viewRect = CGRectMake(250, 100, 30, 30);
    as = [[UIImageView alloc] initWithFrame:viewRect];
    as.backgroundColor = [UIColor clearColor];
    UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"check" ofType:@"png"]];
    as.image = img;
    [self.view addSubview:as];
    [as release];
}

and then show/hide it in -viewDidAppear method:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
    NSLog(@"%@", (test ? @"YES" : @"NO"));
    if(test == YES) {
        as.hidden = NO;
    }
    else {
        as.hidden = YES;
    }
}
like image 124
Narmo Avatar answered Sep 20 '22 02:09

Narmo