Skip to content

Instantly share code, notes, and snippets.

@bestiosdeveloper
Created May 3, 2020 12:56
Show Gist options
  • Save bestiosdeveloper/d9eae502bd46f0ff479ef22a9ea6cbda to your computer and use it in GitHub Desktop.
Save bestiosdeveloper/d9eae502bd46f0ff479ef22a9ea6cbda to your computer and use it in GitHub Desktop.
Custom class inherited from UIButton, used to achieve UIButton's frames changing while resizing content size.
//
// CustomButton.swift
// AccessibilityDemo
//
// Created by Pramod Kumar on 03/05/20.
// Copyright © 2020 Pramod Kumar. All rights reserved.
//
import UIKit
class CustomButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() -> Void {
titleLabel?.adjustsFontForContentSizeCategory = true
titleLabel?.textAlignment = .center
if let ttlLbl = titleLabel {
let leadingConstraint = NSLayoutConstraint(item: ttlLbl, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: ttlLbl, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: ttlLbl, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: ttlLbl, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment