iOS Development

ios – Learn how to setup all UI parts by code with @IBDesignable to XIB from ViewController? / Swift 5



Howdy everybody!) Want some assist!)

In my undertaking I’ve TabBarController which is most important controller.

For TabBarController I created customized viewController like LinkFieldVC, which has awakeFromNib() and cargo XIB.

How can I setup all UI parts by code with @IBDesignable to XIB from LinkFieldVC.


Primary TabBarController:


import UIKit

class MainTabBarController: UITabBarController {

let linkVC = LinkField()

override func viewDidLoad() {
    tremendous.viewDidLoad()

    generateTabBar()
    setTabBarAppearence()
}

personal func generateTabBar() {
    viewControllers = [
        generateViewController(viewController: linkVC, title: "Link", image: UIImage(systemName: "link"))
    ]
}

personal func generateViewController(viewController: UIViewController, title: String, picture: UIImage?) -> UIViewController {
    viewController.tabBarItem.title = title
    viewController.tabBarItem.picture = picture
    return viewController
}

personal func setTabBarAppearence() {
    let positionOnX: CGFloat = 10
    let positionOnY: CGFloat = 15
    let width = tabBar.bounds.width - positionOnX * 2
    let peak = tabBar.bounds.peak + positionOnY * 2
    
    let roundLayer = CAShapeLayer()
    
    let bezierPath = UIBezierPath(
        roundedRect: CGRect(x: positionOnX, y: tabBar.bounds.minY - positionOnY, width: width, peak: peak),
        cornerRadius: peak / 2
    )
    roundLayer.path = bezierPath.cgPath
    
    tabBar.layer.insertSublayer(roundLayer, at: 0)
    
    tabBar.itemWidth = width / 10
    tabBar.itemPositioning = .centered
    
    roundLayer.fillColor = #colorLiteral(pink: 1, inexperienced: 1, blue: 1, alpha: 1)
    
    tabBar.tintColor = #colorLiteral(pink: 0.2849253164, inexperienced: 0.1806431101, blue: 0.5, alpha: 1)
    tabBar.unselectedItemTintColor = #colorLiteral(pink: 0.777954638, inexperienced: 0.6659961939, blue: 0.9842274785, alpha: 1)
}
}

LinkField ViewController:


import UIKit
import Basis
import SafariServices

class LinkField: UIViewController, UITextFieldDelegate {

//MARK: Textual content area
personal let linkTextField: UITextField = {
    let textField = UITextField(body: CGRect(x: 0, y: -0, width: 355, peak: 40))
    textField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0)
    textField.font = UIFont(title: "Roboto-Common", measurement: 17)
    textField.placeholder = "www.instance.com"
    textField.backgroundColor = .clear
    textField.autocorrectionType = .no
    textField.keyboardType = .default
    textField.returnKeyType = .accomplished
    textField.clearButtonMode = .all the time
    textField.contentVerticalAlignment = .heart
    return textField
}()

//MARK: Textual content area underlayer view
personal let textFieldView: UIView = {
    let view = UIView(body: CGRect(x: 0, y: -0, width: 370, peak: 40))
    view.backgroundColor = #colorLiteral(pink: 0, inexperienced: 0, blue: 0, alpha: 0)
    view.layer.cornerRadius = 10
    view.layer.borderWidth = 1
    view.layer.borderColor = #colorLiteral(pink: 0, inexperienced: 0, blue: 0, alpha: 1)
    return view
}()

//MARK: Textual content area information label
personal let textFieldInfoLabel: UILabel = {
    let label = UILabel(body: CGRect(x: 12, y: -12, width: 28, peak: 16))
    label.backgroundColor = .white
    label.layer.cornerRadius = 3
    label.layer.borderWidth = 1
    label.layer.borderColor = #colorLiteral(pink: 0, inexperienced: 0, blue: 0, alpha: 1)
    label.font = UIFont(title: "Roboto-Common", measurement: 11)
    label.textual content = "Hyperlink"
    label.textAlignment = .heart
    label.textColor = #colorLiteral(pink: 0, inexperienced: 0, blue: 0, alpha: 1)
    return label
}()

//MARK: Awake from Nib
override func awakeFromNib() {
    tremendous.awakeFromNib()
    guard let customView = self.view else { return }
    Bundle.most important.loadNibNamed("LinkField", proprietor: self, choices: nil)
    customView.superview?.insertSubview(self.view, aboveSubview: customView)
    customView.removeFromSuperview()
    
    linkTextField.delegate = self
    self.view.addSubview(textFieldView)
    self.view.addSubview(textFieldInfoLabel)
    self.view.addSubview(linkTextField)
}


func openLink(textual content: String) {
    if let urlString = linkTextField.textual content {
        let url: URL?
        if urlString.hasPrefix("https://") {
            url = URL(string: urlString)
        } else {
            url = URL(string: "https://" + urlString)
        }
        
        if let url = url {
            
            DispatchQueue.most important.asyncAfter(deadline: .now() + 2) {
                let sfViewController = SFSafariViewController(url: url)
                self.current(sfViewController, animated: true, completion: nil)
                print ("Your hyperlink was opened in SFSafariViewController")
                
            }
        }
        
    }
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
    guard let textual content = linkTextField.textual content else {return true}
    self.openLink(textual content: textual content)

    return true
}
}

Thanks for each reply!)

What's your reaction?

Leave A Reply

Your email address will not be published.