【问题标题】:How to Create a UIButton with Gradient and Highlighting?如何创建具有渐变和突出显示的 UIButton?
【发布时间】:2013-02-04 16:48:37
【问题描述】:

我正在尝试创建一个带有渐变背景的 UIButton。我做得很好,但是按钮在选择时没有突出显示(默认行为是按钮变暗)。

这是我的按钮:

-(UIButton *)createLoginButtonForSize:(CGSize)size {
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.translatesAutoresizingMaskIntoConstraints = FALSE;
    loginButton.layer.cornerRadius = 8;
    loginButton.titleLabel.text = @"Login";

    [loginButton addTarget:self action:@selector(loginCheck:) forControlEvents:UIControlEventTouchUpInside];


    [loginButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[loginButton(WIDTH)]"
                                                                        options:0
                                                                        metrics:@{@"WIDTH": [NSNumber numberWithFloat:size.width]}
                                                                          views:NSDictionaryOfVariableBindings(loginButton)]];

    [loginButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginButton(HEIGHT)]"
                                                                        options:0
                                                                        metrics:@{@"HEIGHT": [NSNumber numberWithFloat:size.height]}
                                                                          views:NSDictionaryOfVariableBindings(loginButton)]];

    CAGradientLayer *layer = [UIColor greenGradient];
    layer.frame = CGRectMake(0, 0, size.width, size.height);
    layer.cornerRadius = 8;

    [loginButton.layer insertSublayer:layer atIndex:0];

    return loginButton;
}

我需要自己处理突出显示吗?

【问题讨论】:

    标签: ios objective-c uibutton cagradientlayer


    【解决方案1】:

    是的,您需要自己处理突出显示。不过,与其编写自己的代码,不如查看 Jeff Lamarche 非常易于使用的 iPhone Gradient Buttons Project。它完全符合您的要求。它只有 2 个文件,因此很容易合并到您的项目中:

    http://code.google.com/p/iphonegradientbuttons/source/browse/trunk/Classes/GradientButton.h http://code.google.com/p/iphonegradientbuttons/source/browse/trunk/Classes/GradientButton.m

    以下截图取自Jeff's Blog discussing the project

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 2020-07-25
      • 1970-01-01
      相关资源
      最近更新 更多