ViewControllerExtensions.swift 2.52 KB
//
//  ViewControllerExtensions.swift
//  WarplySDKFrameworkIOS
//
//  Created by Manos Chorianopoulos on 5/5/22.
//

import UIKit

extension UIViewController {
    func setBackButton() {
        let uiscreen: CGRect = UIScreen.main.bounds
        
        let backButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
        backButton.frame = CGRect(x: 0, y: 0, width: uiscreen.height * 0.025, height: uiscreen.height * 0.02)
        backButton.imageView!.contentMode = .scaleAspectFit
        //backButton.setBackgroundImage(UIImage(named:Assets.Navigation.backButton), for: UIControlState())
        backButton.setImage(UIImage(named: "ic_back"), for: .normal)
//        backButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
        backButton.addTarget(self, action:  #selector(moveToBack(_:)), for: .touchUpInside)
        
        
        backButton.translatesAutoresizingMaskIntoConstraints = false
        // Add width, height constraints
        let widthContraints =  NSLayoutConstraint(item: backButton, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: uiscreen.height * 0.025)
        let heightContraints = NSLayoutConstraint(item: backButton, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: uiscreen.height * 0.02)
        NSLayoutConstraint.activate([heightContraints,widthContraints])
        
        let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: backButton)
        self.navigationItem.setLeftBarButton(leftBarButtonItem, animated: false)
        self.navigationItem.title = ""
    }
    
    
    @objc func moveToBack(_ sender:UIButton){
        self.navigationController?.popViewController(animated: true)
    }
    
    func setNavigationTitle(_ title: String) {
        let uiscreen: CGRect = UIScreen.main.bounds
        
        let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: uiscreen.width * 0.7, height: uiscreen.height * 0.03))
        titleLabel.text = title
        titleLabel.textColor = UIColor(red: 0.21, green: 0.32, blue: 0.41, alpha: 1.00)
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        titleLabel.adjustsFontSizeToFitWidth = true
        titleLabel.textAlignment = .center
        self.navigationItem.titleView = titleLabel
        
    }
}