2020 - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

27 Apr 2020

Add button programmatically using Autolayouts Swift 5 - iOS

4/27/2020 03:37:00 am 0
Creating a button using autolayouts is quite simple. In this article, I will show how to add a button programmatically and in addition to that, we will learn about all attributes like color, font, image, etc..


Add button programatically using Autolayouts Swift 5 - iOS


Create UIButton:

First of all, create a new button object of type UIButton, and also set translatesAutoresizingMaskIntoConstraints to false. Then give the auto layouts as follow:

let swiftButton = UIButton()
swiftButton.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(swiftButton)
swiftButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
swiftButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
swiftButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
swiftButton.heightAnchor.constraint(equalToConstant: 50).isActive = true


Add a title to Button:

swiftButton.setTitle("Thunder", for: .normal)

Add a title to Button:

Change button title Font & Size:

swiftButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)


Change button title Font & Size:

Change Button background color:

swiftButton.backgroundColor = UIColor.white

Change Button background color:

Change Button title color:

swiftButton.setTitleColor(UIColor.black, for: .normal)

Change Button title color:

Add image to button:

swiftButton.setImage(UIImage(named: "thunder"), for: .normal)

Add image to button:

Change image color in Button:

swiftButton.tintColor = UIColor.red


 Change image color in Button:

Space between button image and title:


swiftButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)

Space between button image and title:

Add tap action on Button:

swiftButton.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)



@objc func buttonTapped(sender: UIButton) {

    print("Button tapped")

}

Change Button title color On Tapped or Selected:

swiftButton.setTitleColor(UIColor.green, for: .highlighted)

Change Button title color On Tapped or Selected:
 

Conclusion:

As a result, we learn how to create a button programmatically & setting up all button properties.



Read More

21 Apr 2020

Dismiss multiple view controllers swift

4/21/2020 02:07:00 am 0
In order to dismiss more than one view controller, here is the small UIViewController extension that can work for many cases.

Cases like:

  • Dismiss to the particular view controller
  • Dismiss 2 or 3 or 4 or many view controllers (By count)
  • Dismiss to root view controller

Dismiss multiple view controllers swift

First of all, add this extension:

extension UIViewController {
    
    func dismissTo(vc: UIViewController?, count: Int?, animated: Bool, completion: (() -> Void)? = nil) {
        var loopCount = 0
        var dummyVC: UIViewController? = self
        for _ in 0..<(count ?? 100) {
            loopCount = loopCount + 1
            dummyVC = dummyVC?.presentingViewController
            if let dismissToVC = vc {
                if dummyVC != nil && dummyVC!.isKind(of: dismissToVC.classForCoder) {
                    dummyVC?.dismiss(animated: animated, completion: completion)
                }
            }
        }
        
        if count != nil {
            dummyVC?.dismiss(animated: animated, completion: completion)
        }
    }
    
}

Add this extension, and use it as follows...

Dismiss to particular view controller:

self.dismissTo(vc: SecondVC(), count: nil, animated: true)

Dismiss by count(1 to 100):

self.dismissTo(vc: nil, count: 3, animated: true)

Dismiss to root view controller:

self.dismissTo(vc: self.view.window?.rootViewController, count: nil, animated: true)

Finally, here is the sample project with four view controllers, download and check the example below.


Read More

Post Top Ad