ios - 如何在设置按钮 alpha 的同时更改 UIButton 的 alpha

标签 ios objective-c uibutton

如图所示,我有 2 个具有 0.5 alpha 和 1 alpha 的按钮。我想将第一张图片中标题的 alpha 更改为 1,这可能吗?

到目前为止,我尝试了这些都不起作用:

button.titleLabel.alpha = 1;  
[button setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1] forState:UIControlStateNormal];

enter image description here

UIButton *button = (UIButton *)view;
if(self.currentArray[index][2] != NULL) //before it was if (button == nil)
{
    NSString *name = [[NSString alloc] initWithString:self.currentArray[index][2]];
    UIImage *image = [UIImage imageNamed:self.currentArray[index][5]];
    button = [UIButton buttonWithType:UIButtonTypeCustom];

    int extraLeftInset = 0;
    if ([self.currentArray[index][6] isEqualToString:@"true"]) {
        //show it
    }else if([self.currentArray[index][7] isEqualToString:@"true"]){

    }else{
        UIImage *locked = [UIImage imageNamed:@"fruitify_locked.png"];
        button.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
        [button setImage:locked forState:UIControlStateNormal];
        extraLeftInset = - 256; //size of locked
        button.backgroundColor = [UIColor clearColor];
        button.alpha = 0.5;
    }

    button.frame = CGRectMake(0.0, 0.0, 56, 56);
    button.tag = index;

    [button setBackgroundImage:image forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIEdgeInsets buttonInset = UIEdgeInsetsMake(30, -2 + extraLeftInset, -20, -2);
    [button setTitleEdgeInsets:buttonInset];
    [button setTitle:name forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 8];
    button.titleLabel.lineBreakMode   = NSLineBreakByWordWrapping;


    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}

最佳答案

执行此操作的一种方法是调整图像的 alpha 值。这可以通过将图像传递给此方法来完成,

-(UIImage *)image:(UIImage *) image  withAdjustedAlpha:(CGFloat) newAlpha{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    [image drawAtPoint:CGPointZero blendMode:kCGBlendModeCopy alpha:newAlpha];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

关于ios - 如何在设置按钮 alpha 的同时更改 UIButton 的 alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24341597/

相关文章:

iphone - 应用退出 [managedObjectContext 保存 :] without an error

swift - 使用嵌套函数作为 `UIButton` 的操作

javascript - react-native 中的 loadingIndicatorSource 和 defaultSource 属性有什么区别?

Iphone Simulator - 与设备相比分配太多内存并且运行缓慢

objective-c - 公共(public)读取, "private"保留属性

objective-c - 在 Cocoa 中捕获所有多点触控板输入

ios - 如何更改 Popover View 的大小

ios - Xamarin iOS后台上传任务(iOS 7)——DidFinishEventsForBackgroundSession

ios - 在 UI 上调用事件方法

iphone - 从 objective-c 中的另一个按钮打开一个按钮