iosobjective-cbuttonuibuttontweak

How custom the design of UIButton in objective-C?


I need help to create a custom UIButton, I'm creating a tweak, i have an custom uiview and i know how to add a button on it.

But i don't like the blue and tiny stock style of it, I want to custom my button like my UIView with color style and size but don't know how to.

I want something like this : Image of button style I want


Solution

  • The Custom Button style can be achieved like below.

    Objective-C

    CustomButton.h

    #import <Foundation/Foundation.h>
    
    @interface UIImage (Utils)
    
    + (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color;
    
    @end
    
    @interface CustomButton : UIButton
    
    - (instancetype)initWithCoder:(NSCoder *)coder;
    
    @end
    

    CustomButton.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
    

    Swift

    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
        }
    }
    

    Drag and drop UIButton to your UIView in your storyboard and make class in Identity Inspector as CustomButton and add constraints as required. Thats it.

    Please find the resultant screenshot. Hope it helps.

    enter image description here