9 Notes June 2021

1. Extension: Layout-Helper

Layout Helper is created to solve the issue of constraint based layout where the developer are bound by the size classes for the same size classes but haveing different screen sizes. Layout Helper will give Developer the freedom to update Constraints, Fonts of various UI element for every single device.

  
//  NSLayoutHelper.swift
//  Layout Helper
//
//  Created by Kuldeep Tanwar on 4/15/19.
//  Copyright © 2019 Kuldeep Tanwar. All rights reserved.
import UIKit
@IBDesignable class NSLayoutHelper : NSLayoutConstraint {
    @IBInspectable var iPhone4s: CGFloat = 0.0 {
        didSet { deviceConstant(.i3_5Inch,value:iPhone4s) }
    }
    @IBInspectable var iPhoneSE: CGFloat = 0.0 {
        didSet { deviceConstant(.i4Inch,value:iPhoneSE) }
    }
    @IBInspectable var iPhoneSE2G: CGFloat = 0.0 {
        didSet { deviceConstant(.i4_7Inch,value:iPhoneSE2G) }
    }
    @IBInspectable var iPhone8Plus: CGFloat = 0.0 {
        didSet { deviceConstant(.i5_5Inch,value:iPhone8Plus) }
    }
    @IBInspectable var iPhone11Pro: CGFloat = 0.0 {
        didSet { deviceConstant(.i5_8Inch,value:iPhone11Pro) }
    }
    @IBInspectable var iPhone11: CGFloat = 0.0 {
        didSet { deviceConstant(.i6_1Inch,value:iPhone11) }
    }
    @IBInspectable var iPhone11Max: CGFloat = 0.0 {
        didSet { deviceConstant(.i6_5Inch,value:iPhone11Max) }
    }
    @IBInspectable var iPhone12Mini: CGFloat = 0.0 {
        didSet { deviceConstant(.i5_4Inch,value:iPhone12Mini) }
    }
    @IBInspectable var iPhone12: CGFloat = 0.0 {
        didSet { deviceConstant(.i6_1Inch,value:iPhone12) }
    }
    @IBInspectable var iPhone12ProMax: CGFloat = 0.0 {
        didSet { deviceConstant(.i6_7Inch,value:iPhone12ProMax) }
    }
    @IBInspectable var iPadMini: CGFloat = 0.0 {
        didSet { deviceConstant(.i7_9Inch,value:iPadMini) }
    }
    @IBInspectable var iPadPro9_7: CGFloat = 0.0 {
        didSet { deviceConstant(.i9_7Inch,value:iPadPro9_7) }
    }
    @IBInspectable var iPadPro10_5: CGFloat = 0.0 {
        didSet { deviceConstant(.i10_5Inch,value:iPadPro10_5) }
    }
    @IBInspectable var iPadPro11: CGFloat = 0.0 {
        didSet { deviceConstant(.i11_Inch,value:iPadPro11) }
    }
    @IBInspectable var iPadPro12_9: CGFloat = 0.0 {
        didSet { deviceConstant(.i12_9Inch,value:iPadPro12_9) }
    }
    // Helpers
    open func deviceConstant(_ device:UIDeviceSize,value:CGFloat) {
        if deviceSize == device {
            constant = value
        }
    }
}

source: https://github.com/tryWabbit/Layout-Helper
link medium: https://medium.com/swift-blondie/5-swift-extensions-that-will-make-your-life-easier-41ca42d60946


2. Extension:  Border-Button

In case you are looking to make a fancy red color rounded button with a clear background, this extension will make it easier. You can totally hard code this style but it can get hectic if a single viewcontroller has minimum of 7–8 buttons.3. ???

import Foundation
import UIKit

public enum UIButtonBorderSide {
    case Top, Bottom, Left, Right
}

extension UIButton {
    
    public func addBorder(side: UIButtonBorderSide, color: UIColor, width: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        
        switch side {
        case .Top:
            border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
        case .Bottom:
            border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
        case .Left:
            border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
        case .Right:
            border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
        }
        
        self.layer.addSublayer(border)
    }
}

source: https://gist.github.com/Isuru-Nanayakkara/496d5713e61125bddcf5
link medium: https://medium.com/swift-blondie/5-swift-extensions-that-will-make-your-life-easier-41ca42d60946


3. Extension: Resizing-UIImage

This extension will help with resizing the image and make it fit especially for the small dimensions. It is a very helpful extension also when used for the images pulled from the backend. It renders the image in the format needed to be shown.

import Foundation
import ImageIO
import UIKit

extension UIImage {
    func resized(withPercentage percentage: CGFloat, isOpaque: Bool = true) -> UIImage? {
        let canvas = CGSize(width: size.width * percentage, height: size.height * percentage)
        let format = imageRendererFormat
        format.opaque = isOpaque
        return UIGraphicsImageRenderer(size: canvas, format: format).image {
            _ in draw(in: CGRect(origin: .zero, size: canvas))
        }
    }
    func resized(toWidth width: CGFloat, isOpaque: Bool = true) -> UIImage? {
        let canvas = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
        let format = imageRendererFormat
        format.opaque = isOpaque
        return UIGraphicsImageRenderer(size: canvas, format: format).image {
            _ in draw(in: CGRect(origin: .zero, size: canvas))
        }
    }
}


source: https://github.com/ynnenu/Swift/blob/main/UImage%20Extension

link medium: https://medium.com/swift-blondie/5-swift-extensions-that-will-make-your-life-easier-41ca42d60946

4. Error install pods

when installing pods in terminal I've got this error

  app git:(e72f00ce0) arch -x86_64 pod install

Analyzing dependencies

[!] CocoaPods could not find compatible versions for pod "Firebase/Analytics":

  In snapshot (Podfile.lock):

    Firebase/Analytics (= 8.2.0)


  In Podfile:

    Firebase/Analytics


None of your spec sources contain a spec satisfying the dependencies: `Firebase/Analytics, Firebase/Analytics (= 8.2.0)`.


You have either:

 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.

 * mistyped the name or version.

 * not added the source repo that hosts the Podspec to your Podfile.


------------------------------------------------------------------------------------------------------------------------
solution :

  app git:(e72f00ce0) arch -x86_64 pod install --repo-update

Updating local specs repositories

Analyzing dependencies

Downloading dependencies

Installing Crisp 1.0.14 (was 1.0.13)

Installing FBSDKCoreKit 11.0.1 (was 5.15.1)

Installing FBSDKCoreKit_Basics (11.0.1)

Installing FBSDKLoginKit 11.0.1 (was 5.15.1)

Installing FBSDKShareKit 11.0.1 (was 5.15.1)

Installing Firebase 8.2.0 (was 8.0.0)

Installing FirebaseABTesting 8.2.0 (was 8.0.0)


5. Error When build project get this FBSDKCoreKit-Swift.h

When I've build the project get some error this error below


How to solved:

- clean build project shift + cmd + K

- build project again or you can install pod before build project pod install

- Run app is must be running normal


6. Missing UIImageView when runnig app

I'll run the project and missing image view but when I've checking data, asset adn UI in xib file i'ts there , but image not show in my app 

How to solved:

Set setup in klik imageview select attribute inspector change data in class like this image preview
- change class

- change module (mostly auto change modelu when you change class)

- change Image name string 

Use this extension code:

import UIKit

@IBDesignable class UIImageViewHelper: UIImageView {
    @IBInspectable var imageName: String = ""
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    
    public override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }
    
    private func setup() {
        self.image = UIImage(named: UIImage.Asset(rawValue: imageName)!, in: ImageUI.self)
    }
}


Image Before Fixing




Image After Fixing



7.  Extension string number to bool

This extension bool number if you have int "0" in string it's change to FALSE other word in string is TRUE.


extension Bool {
    init(from: String) {
        self = (from as NSString).boolValue
    }
}

// If use "0" it's can be false
let stringInt = Bool(from: "0") 
print(stringInt) // false

// If use "01" or "1" number and use word string it's can be true 
let stringInt = Bool(from: "01") 
print(stringInt) // true

let stringInt = Bool(from: "you and?") 
print(stringInt) // true


8. When running git rebase get error "fatal : No rebase in progress?"

flow get that error in terminal
$ git add .
$ git rebase --continue
fatal: No rebase in progress?

the problem is because already in rebase

9. Error build FBSDK When Build pod CleverTap

Condition is when use project module with patern VIP after install pod I've found this error like this image below


when klik module.modulemap this error can see



solution is when use module like this you can set in main target 'Project' do and sub target
target 'SubModule' do

// install pod again
$ pod install

// after installing pod clean build xcode
// and build app









No comments: