0%

25 Swift Task

Present View Example


ViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import UIKit

class ViewController: UIViewController {

let label = UILabel()
let button = UIButton()

var number = 0

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .random()

setupLabel()
setupButton()
}

func setupLabel() {
label.frame.size = CGSize(width: view.frame.width, height: 100)
label.center = view.center
label.text = "\(number)"
label.textColor = .random()
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 90)

view.addSubview(label)
}

func setupButton() {
button.frame = CGRect(x: 0, y: view.bounds.height - 200,
width: view.bounds.width, height: 100)
button.setTitle("Go to Next View", for: .normal)
button.setTitleColor(.random(), for: .normal)
button.titleLabel?.font = UIFont(name: "Arial", size: 20)
button.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
button.titleLabel?.textAlignment = NSTextAlignment.center
button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)

view.addSubview(button)
}

@objc
func didTapButton(_ sender: UIButton) {
let nextVC = NextViewController()
nextVC.modalPresentationStyle = .fullScreen
nextVC.modalTransitionStyle = .flipHorizontal

number += 1
present(nextVC, animated: true, completion: nil)
}

}

NextViewController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import UIKit

class NextViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .random()

setupButton()
}

override func viewWillAppear(_ animated: Bool) {
setupLabel()
}

func setupLabel() {
let label = UILabel()

guard let vc = presentingViewController as? ViewController else { return }

label.frame.size = CGSize(width: view.frame.width, height: 100)
label.center = view.center
label.text = "\(vc.number)"
label.textColor = .random()
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 90)

view.addSubview(label)
}

func setupButton() {
let button = UIButton()

button.frame = CGRect(x: 0, y: view.bounds.height - 200, width: view.bounds.width, height: 100)
button.setTitle("Back to Pervious View", for: .normal)
button.setTitleColor(.random(), for: .normal)
button.titleLabel?.font = UIFont(name: "Arial", size: 20)
button.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
button.titleLabel?.textAlignment = NSTextAlignment.center
button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)

view.addSubview(button)
}

@objc
func didTapButton(_ sender: UIButton) {
guard let vc = presentingViewController as? ViewController else { return }

vc.number += 1
vc.label.text = "\(vc.number)"
vc.label.textColor = .random()
vc.button.setTitleColor(.random(), for: .normal)
vc.view.backgroundColor = .random()

dismiss(animated: true, completion: nil)
}

}

After read these codes you might have a question about .random(), because UIColor has no random method. I’ll let you know how to bring random method in your project. It’ll make your app more colorful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
extension CGFloat {
static func random() -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UInt32.max)
}
}

extension UIColor {
static func random() -> UIColor {
return UIColor(red: .random(),
green: .random(),
blue: .random(),
alpha: 1.0)
}
}

here is the result of these codes

Example Video