directory
directory

Reputation: 3167

Changing image alpha

In my application I am trying to change the alpha of a icon (image view) in my input field when it's selected, filled or leaved blank.

I've written this code but nothing is happening, I am sure I wrote the image names as they are named in my image directory.

Sample code;

- (IBAction)passwordInputTab:(id)sender {

    UIImageView *imageLockIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lock-icon.png"]];
    imageLockIcon.alpha = 1;
    //self.txtPassword.alpha = 1;

    if ([[self.txtUsername text] isEqualToString:@""]) {
        //self.txtUsername.alpha = 0.5;
        UIImageView *imageUserIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user-icon.png"]];
        imageUserIcon.alpha = 0.5;
    }
}

- (IBAction)usernameInputTab:(id)sender {

    UIImageView *imageUserIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user-icon.png"]];
    imageUserIcon.alpha = 1;

    NSLog(@"==> %@", imageUserIcon);

    //userIcon.alpha = 0.1;

    if ([[self.txtPassword text] isEqualToString:@""]) {

        UIImageView *imageLockIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lock-icon.png"]];
        imageLockIcon.alpha = 0.5;
        NSLog(@"==> %@", imageLockIcon);
        //self.txtPassword.alpha = 0.5;
    }
}

Log;

2014-09-18 21:47:10.882 ...[1280:60b] ==> <UIImageView: 0x14e8dd90; frame = (0 0; 17 16); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x14e8de10>>
2014-09-18 21:47:10.884 ...[1280:60b] ==> <UIImageView: 0x14d66770; frame = (0 0; 12 16); alpha = 0.5; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x14da43d0>>

Upvotes: 0

Views: 62

Answers (2)

Marck Gorgon
Marck Gorgon

Reputation: 73

You are creating new UIImageViews, if you created them in the InterfaceBuilder you need to create a @property for each one of them and link the IBOutlets on your storyboard/xib.

If you want to create the UIImageViews programmatically you are only forgetting to add:

[self.view addSubview:YOUR_IMAGEVIEW];

Example:

[self.view addSubview:imageUserIcon];

Upvotes: 1

Casey Fleser
Casey Fleser

Reputation: 5787

I see that you're creating a UIImageView but it is not being added to the view hierarchy. If you are trying to change the alpha on an existing UIImageView you'll need to provide a reference to it via an IBOutlet to adjust its properties.

Upvotes: 0

Related Questions