Objective c 更改图像alpha
在我的应用程序中,我试图更改输入字段中图标(图像视图)的alpha值 它被选中、填充或留空 我已经写了这段代码,但是什么都没有发生,我确信我写的图像名称与它们在我的图像目录中的名称相同 样本代码Objective c 更改图像alpha,objective-c,iphone,xcode,transparency,Objective C,Iphone,Xcode,Transparency,在我的应用程序中,我试图更改输入字段中图标(图像视图)的alpha值 它被选中、填充或留空 我已经写了这段代码,但是什么都没有发生,我确信我写的图像名称与它们在我的图像目录中的名称相同 样本代码 - (IBAction)passwordInputTab:(id)sender { UIImageView *imageLockIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lock-icon.png"]];
- (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;
}
}
日志
2014-09-1811:47:10.882…[1280:60b]=>
2014-09-18 21:47:10.884…[1280:60b]=>
我看到您正在创建UIImageView,但它没有添加到视图层次结构中。如果您试图更改现有UIImageView上的alpha,则需要通过IBOutlet提供对其的引用以调整其属性。您正在创建新的UIImageView,如果您在InterfaceBuilder中创建了它们,则需要为每一个UIImageView创建一个@property
,并在故事板/xib上链接IBOutlet
如果要以编程方式创建UIImageView,您只会忘记添加:
[self.view addSubview:YOUR_IMAGEVIEW];
例如:
[self.view addSubview:imageUserIcon];
谢谢你,马克!由IBOUtlet解决!:)
[self.view addSubview:imageUserIcon];