Posts

FEATURED

Password With Show hide button

import UIKit class PasswordTextField: UITextField { private let toggleButton = UIButton(type: .custom) override init(frame: CGRect) { super.init(frame: frame) setupView() } required init?(coder: NSCoder) { super.init(coder: coder) setupView() } private func setupView() { isSecureTextEntry = true borderStyle = .roundedRect rightViewMode = .always setupToggleButton() } private func setupToggleButton() { let buttonSize = CGSize(width: 24, height: 24) toggleButton.setImage(UIImage(systemName: "eye.fill"), for: .normal) toggleButton.frame = CGRect(origin: .zero, size: buttonSize) toggleButton.contentMode = .scaleAspectFit toggleButton.addTarget(self, action: #selector(togglePasswordVisibility), for: .touchUpInside) rightView = toggleButton addConstraints() } private func addConstraints() {

Helper Constraint

import UIKit extension UIView { func constrain(to view: UIView, withInsets insets: UIEdgeInsets = .zero) { translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: view.topAnchor, constant: insets.top), bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -insets.bottom), leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: insets.left), trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -insets.right) ]) } func constrain(size: CGSize) { translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ widthAnchor.constraint(equalToConstant: size.width), heightAnchor.constraint(equalToConstant: size.height) ]) } func center(in view: UIView, xOffset: CGFloat = 0, yOffset: CGFloat = 0) { translatesAutoresizingMaskIntoConstraints

iOS Swift Custom Circular Progress bar

import UIKit class CircularProgressView: UIView { // First create two layer properties private var circleLayer = CAShapeLayer() private var progressLayer = CAShapeLayer() override init(frame: CGRect) { super.init(frame: frame) createCircularPath() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) createCircularPath() } func createCircularPath() { let circularPath = UIBezierPath( arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: 80, startAngle: -.pi / 2, endAngle: 3 * .pi / 2, clockwise: true) circleLayer.path = circularPath.cgPath circleLayer.fillColor = UIColor.clear.cgColor circleLayer.lineCap = .round circleLayer.lineWidth = 20.0 circleLayer.strokeColor = UIColor.black.cgColor progressLayer.path = circularPath.cgPath progressLayer.fillColor = UIColor.clear.cgColor progressLayer.lineCap = .round progressLayer.lineWidth = 10.0 progressLayer.strokeEnd = 0 pro

View Shadow and radius

lazy var containerView: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = .white view.layer.shadowColor = UIColor.gray.cgColor view.layer.shadowRadius = 2 view.layer.shadowOpacity = 1 view.layer.shadowOffset = CGSize(width: 1, height: 1) view.layer.cornerRadius = 8 view.layer.masksToBounds = false return view }()

CombinePathView

class CombinedPathView: UIView { private ( set ) var cornerBubbleWidth: CGFloat = 16 init(frame: CGRect, radius: CGFloat = 16) { super.init(frame: frame) backgroundColor = .clear self.cornerBubbleWidth = radius } required init?(coder: NSCoder) { super.init(coder: coder) } override func draw(_ rect: CGRect) { super.draw(rect) let path1 = UIBezierPath( roundedRect: CGRect( x: 0, y: 0, width: frame.width, height: frame.height - cornerBubbleWidth ), byRoundingCorners: [.topLeft, .topRight, .bottomLeft], cornerRadii: CGSize(width: cornerBubbleWidth, height: cornerBubbleWidth)) let path2 = UIBezierPath() path2.move( to: CGPoint( x: frame.width - (cornerBubbleWidth * 2), y: frame.height - cornerBubbleWidth )

iOS Swift Loading subview animation

import UIKit class CircleLoading : UIView {          init () {         super . init ( frame : . zero )         self . setupViews ()     }          required init ?( coder : NSCoder ) {         super . init ( coder : coder)         self . setupViews ()     }          private func setupViews () {         self . backgroundColor = . clear                  var delay = 0.0         [- 50.0 , 0.0 , 50.0 ]. forEach { anchorX in             let viewCircle = createCircle ( anchorX : anchorX)             UIView . animate (                 withDuration : 0.5 ,                 delay : delay,                 options : [. autoreverse , . repeat ],                 animations : {                 viewCircle. frame . origin = CGPoint (                     x : viewCircle. frame . origin . x ,                     y : 50 )                 viewCircle. alpha = 0.3             }, completion : nil )             delay += 0.2         }     }          private func createCircle ( anchorX : CGFloat ) -&g

iOS Swift Codable Different json fields manual parsing

Image
  Property   Children   tidak ada di api: Parent  ->  /api/user_parents seperti berikut: tetapi ada di api: Parent with children  ->  /api/user_parents?include=children seperti berikut: Agar Property  Children  tidak gagal parsing, menurut dokumentasi, maka semua property Codable harus diisi secara manual, di fungsi berikut: public required convenience init(from decoder: Decoder) Agar generate parse dapat dilakukan dengan mudah, maka lakukan langkah berikut, 1. generate model seperti biasa di  https://app.quicktype.io/  hasilnya seperti berikut 2. Buatlah Convenience Init seperti berikut 3. Generate json yang tadi di website: https: //json2kt .com/json-to-swift .php Sehingga properties di atas akan di generate kan `decodeIfPresent` nya 4. Copy body dari `init(from)` dan paste di model kita, sehingga akhirnya menjadi seprti berikut: