【问题标题】:How custom the design of UIButton in objective-C? [closed]Objective-C中如何自定义UIButton的设计? [关闭]
【发布时间】:2019-09-03 16:01:36
【问题描述】:

我需要帮助来创建自定义 UIButton,我正在创建一个调整,我有一个自定义 uiview,并且我知道如何在其上添加一个按钮。

但我不喜欢它的蓝色和微小的股票样式,我想自定义我的按钮,如 UIView 的颜色样式和大小,但不知道如何。

我想要这样的东西:Image of button style I want

【问题讨论】:

  • UIButton,样式设置为自定义并更改其颜色属性。对于圆角设置图层的圆角半径属性。

标签: ios objective-c button uibutton tweak


【解决方案1】:

自定义按钮样式可以如下实现。

Objective-C

自定义按钮.h

#import <Foundation/Foundation.h>

@interface UIImage (Utils)

+ (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color;

@end

@interface CustomButton : UIButton

- (instancetype)initWithCoder:(NSCoder *)coder;

@end

自定义按钮.m

#import <UIKit/UIKit.h>
#import "CustomButton.h"

@implementation UIImage (Utils)

+ (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color
{
    UIGraphicsBeginImageContextWithOptions(size, true, 0.0);
    [color setFill];
    UIRectFill(CGRectMake(0.0, 0.0, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

@implementation CustomButton: UIButton

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {

        UIImage *bgImage = [UIImage imageWithSize:self.bounds.size color:UIColor.blackColor];
        [self setBackgroundImage:bgImage forState:UIControlStateNormal];
        [self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        self.layer.cornerRadius = 40.0;
        self.layer.masksToBounds = YES;
        self.titleLabel.font = [UIFont systemFontOfSize:18.0 weight:UIFontWeightRegular];
    }
    return self;
}

@end

斯威夫特

CustomButton.swift

struct AppStyles {

    struct ActionButton {
        static let backgroundColor = UIColor.black
        static let textColor = UIColor.white
        static let font = UIFont.systemFont(ofSize: 18.0, weight: .regular)
    }
}

extension UIImage {

    class func imageWithSize(_ size: CGSize, color: UIColor) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
        color.setFill()
        UIRectFill(CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

class CustomButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let style: AppStyles.ActionButton.Type = AppStyles.ActionButton.self
        let backgroundImage = UIImage.imageWithSize(self.bounds.size, color: style.backgroundColor)
        self.setBackgroundImage(backgroundImage, for: UIControl.State())
        self.setTitleColor(style.textColor, for: UIControl.State())
        self.layer.cornerRadius = 40.0
        self.layer.masksToBounds = true


        if let label = self.titleLabel {
            label.font = style.font
        }

        self.isEnabled = true
    }
}

将 UIButton 拖放到情节提要中的 UIView 中,并将 Identity Inspector 中的类设为 CustomButton 并根据需要添加约束。而已。

请找到生成的屏幕截图。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多