ios - PaintCode 中的 UIButton (Swift)

标签 ios swift uiview uibutton paintcode

所以我一直在 Xcode 中使用 PaintCode 和 StyleKits。我做了一个按钮,用它自己的子类创建了一个 UIView,它在我的 ViewController 和我的设备上显示得很好。但是,我想将此 UIView 转换为 UIButton,因为我希望代码在被点击时执行。我该怎么做呢?

谢谢你们,祝你们玩得开心 :)

最佳答案

我使用 PaintCode 的 UIImage 方法,并将该 UIImage 用作 UIButton 的图像。这是我使用的代码,它在 ObjC 中,但应该很容易转换为 Swift。

PaintCode 生成的代码——你不需要写的东西(假装这个类叫做 PaintCodeClass):

+ (UIImage*)imageOfIcon
{
   if (_imageOfIcon)
        return _imageOfIcon;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(30, 30), NO, 0.0f);
    [PaintCodeClass drawIcon];

    _imageOfIcon = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return _imageOfIcon;
}

和我的代码:

UIImage *image = [PaintCodeClass imageOfIcon];
UIButton *button = (initialize, set frame, etc)
[button setImage:image forState:UIControlStateNormal];

有些人更喜欢这样:

[button setBackgroundImage:image forState:UIControlStateNormal];

除此之外,如果你真的想要,你可以这样做:

[button addSubview:whateverThePaintCodeViewIsCalled];

但这不会表现得像一个按钮 - 所以您可能不想那样做。

关于ios - PaintCode 中的 UIButton (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38456784/

相关文章:

ios - UIAlertview 没有正常关闭?

ios - 检测标签何时出现在 SWIFT 屏幕上

iphone - 为 iphone 4 和 5 创建单独的 Storyboard

ios - 按 NULL NSDates 对 NSFetchedResultsController 进行排序

ios - 如何在没有 Cognito 的情况下从 iOS 上传到 AWS S3?

ios - 来自 AVURLAsset 的 URL 的数据 contentsOf 返回 nil

iphone - 从 superView 中删除 UIView

ios - 在 Swift 中保留 UITableView 中的单元格?

swift - 为什么延迟动画和for循环不同步?

ios - 如何使用 Alamofire 将多部分图像上传到服务器