Skip to content

Commit 82f70fe

Browse files
committed
adapts scrollView size to keyboard
1 parent 45da80a commit 82f70fe

5 files changed

Lines changed: 129 additions & 160 deletions

File tree

AnimalsApp/AnimalsApp/Assets.xcassets/Colors/grayTextColor.colorset/Contents.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"color-space" : "srgb",
66
"components" : {
77
"alpha" : "1.000",
8-
"blue" : "0.400",
9-
"green" : "0.400",
10-
"red" : "0.400"
8+
"blue" : "0x66",
9+
"green" : "0x66",
10+
"red" : "0x66"
1111
}
1212
},
1313
"idiom" : "universal"

AnimalsApp/AnimalsApp/DesignSystem/Colors.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ import UIKit
1010
extension UIColor {
1111
static let blueTabBarColor: UIColor? = UIColor(named: "blueTabBarColor")
1212
static let blueTextColor: UIColor? = UIColor(named: "blueTextColor")
13-
13+
14+
static let grayCellFrame: UIColor? = UIColor(named: "grayCellFrame")
15+
static let grayTextColor: UIColor? = UIColor(named: "grayTextColor")
1416
}

AnimalsApp/AnimalsApp/Views/Components/AnimalTableViewCell.xib

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
</constraints>
3232
</imageView>
3333
<button opaque="NO" contentMode="scaleAspectFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="687-rn-Ydp">
34-
<rect key="frame" x="247" y="33" width="40" height="30"/>
34+
<rect key="frame" x="247" y="28" width="40" height="40"/>
3535
<constraints>
36-
<constraint firstAttribute="height" constant="30" id="7rZ-bg-lFP"/>
36+
<constraint firstAttribute="height" constant="40" id="7rZ-bg-lFP"/>
3737
<constraint firstAttribute="width" constant="40" id="i65-df-ED5"/>
3838
</constraints>
39-
<inset key="imageEdgeInsets" minX="35" minY="0.0" maxX="35" maxY="0.0"/>
4039
<state key="normal" image="addFavorite"/>
4140
<connections>
4241
<action selector="favoritePressed:" destination="KGk-i7-Jjw" eventType="touchUpInside" id="X9N-Mv-iRU"/>
@@ -85,10 +84,10 @@
8584
<image name="addFavorite" width="22.5" height="21.5"/>
8685
<image name="imagePlaceholder" width="300" height="300"/>
8786
<namedColor name="blueTextColor">
88-
<color red="0.082352941176470587" green="0.54509803921568623" blue="0.74901960784313726" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
87+
<color red="0.082000002264976501" green="0.54500001668930054" blue="0.74900001287460327" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
8988
</namedColor>
9089
<namedColor name="grayTextColor">
91-
<color red="0.40000000000000002" green="0.40000000000000002" blue="0.40000000000000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
90+
<color red="0.40000000596046448" green="0.40000000596046448" blue="0.40000000596046448" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
9291
</namedColor>
9392
</resources>
9493
</document>

AnimalsApp/AnimalsApp/Views/RegisterViewController/RegisterViewController.swift

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,43 @@ import Alamofire
1010

1111
class RegisterViewController: UIViewController {
1212
// MARK: Properties
13-
var registerVM = RegisterViewModel()
13+
private var registerVM = RegisterViewModel()
1414

1515
// MARK: Outlets
16+
@IBOutlet weak var scrollView: UIScrollView!
1617
@IBOutlet weak var textFieldName: UITextField!
1718
@IBOutlet weak var textFieldImageLink: UITextField!
1819
@IBOutlet weak var textFieldDescription: UITextField!
1920
@IBOutlet weak var textFieldSpecie: UITextField!
2021
@IBOutlet weak var textFieldAge: UITextField!
2122
@IBOutlet weak var buttonRegister: UIButton!
22-
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
23+
// @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
2324

2425
// MARK: Overrides
2526
override func viewDidLoad() {
2627
super.viewDidLoad()
2728
setupUI()
2829
setNavigationItems()
2930
delegateTextField()
30-
activityIndicator.hidesWhenStopped = true
31+
notificationCenter()
32+
// activityIndicator.hidesWhenStopped = true
3133
}
3234

3335
// MARK: Actions
3436
@IBAction func handlerButtonRegister(_ sender: Any) {
37+
3538
guard let name = textFieldName.text?.testIfIsEmpty(),
3639
let description = textFieldDescription.text?.testIfIsEmpty(),
3740
let age = Int(textFieldAge.text ?? ""),
3841
let species = textFieldSpecie.text?.testIfIsEmpty(),
3942
let image = textFieldImageLink.text?.testIfIsEmpty() else {
40-
showAlerts(alertTitle: "Erro", alertMessage: "Preencha todos os dados")
43+
showAlerts(alertTitle: "Erro", alertMessage: "Preencha todos os campos")
4144
return }
4245

43-
activityIndicator.startAnimating()
46+
// activityIndicator.startAnimating()
4447
registerVM.registerAnimal(name: name, description: description, age: age, species: species, image: image) {
45-
self.activityIndicator.stopAnimating()
48+
// self.activityIndicator.stopAnimating()
49+
print("oi")
4650
}
4751
}
4852

@@ -63,16 +67,16 @@ class RegisterViewController: UIViewController {
6367
[textFieldName, textFieldImageLink, textFieldDescription, textFieldSpecie, textFieldAge].forEach { textField in
6468
textField?.layer.cornerRadius = 8
6569
textField?.layer.borderWidth = 1
66-
textField?.layer.borderColor = UIColor.systemGray.cgColor
70+
textField?.layer.borderColor = UIColor.grayCellFrame?.cgColor
6771
textField?.layer.masksToBounds = true
6872
}
6973

70-
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.systemGray]
71-
textFieldName.attributedPlaceholder = NSAttributedString(string: "Nome", attributes:attributes)
72-
textFieldImageLink.attributedPlaceholder = NSAttributedString(string: "Link da imagem", attributes:attributes)
73-
textFieldDescription.attributedPlaceholder = NSAttributedString(string: "Descrição", attributes:attributes)
74-
textFieldSpecie.attributedPlaceholder = NSAttributedString(string: "Espécie", attributes:attributes)
75-
textFieldAge.attributedPlaceholder = NSAttributedString(string: "Idade", attributes:attributes)
74+
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.grayTextColor ?? UIColor.systemGray, NSAttributedString.Key.font: UIFont(name: "OpenSans", size: 16) ?? UIFont.systemFont(ofSize: 16)]
75+
textFieldName.attributedPlaceholder = NSAttributedString(string: " Nome", attributes: attributes)
76+
textFieldImageLink.attributedPlaceholder = NSAttributedString(string: " Link da imagem", attributes: attributes)
77+
textFieldDescription.attributedPlaceholder = NSAttributedString(string: " Descrição", attributes: attributes)
78+
textFieldSpecie.attributedPlaceholder = NSAttributedString(string: " Espécie", attributes: attributes)
79+
textFieldAge.attributedPlaceholder = NSAttributedString(string: " Idade", attributes: attributes)
7680
buttonRegister.layer.cornerRadius = 10
7781
}
7882

@@ -90,6 +94,27 @@ class RegisterViewController: UIViewController {
9094
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
9195
present(alert, animated: true)
9296
}
97+
98+
private func notificationCenter() {
99+
let notificationCenter = NotificationCenter.default
100+
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
101+
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
102+
}
103+
104+
@objc func adjustForKeyboard(notification: Notification) {
105+
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
106+
107+
let keyboardScreenEndFrame = keyboardValue.cgRectValue
108+
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
109+
110+
if notification.name == UIResponder.keyboardWillHideNotification {
111+
scrollView.contentInset = .zero
112+
} else {
113+
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height - view.safeAreaInsets.bottom, right: 0)
114+
}
115+
116+
scrollView.scrollIndicatorInsets = scrollView.contentInset
117+
}
93118
}
94119

95120
extension RegisterViewController: UIGestureRecognizerDelegate, UITextFieldDelegate {

0 commit comments

Comments
 (0)