所以我一直在 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/