9 Notes In February 2020
1. Hide Element on Select tableView cell
source: https://stackoverflow.com/a/34603224/8366535
2. SwiftUI Basic Note
- Parent view just have 10 limits to make child views
example:
example:
- NavigationView : to add navigation on the top controller
- .navigationBarTitle(Text("SwiftUI APP")) : for title navigation in swiftUI, displayMode
- displayMode: .large : for change title size (default is .large), .large is big use big font and .inline is normal font
- .colorScheme(.dark) : change theme .dark or .light mode theme
- .foregroundColor(.blue) : for text color in form
- Create a new variable must be use @State(property wrapper), if you did not use a that must be an error like this below
Left side of mutating operator isn't mutable: 'self' is immutable
- In SwiftUI Segmented Control create with picker
- Color is a View (Color.red), example :
- every time you write some View. This means “one specific type that conforms to the View protocol(source: https://www.hackingwithswift.com/books/ios-swiftui/why-does-swiftui-use-some-view-for-its-view-type)
3. Custom leftBarButton can still swipe left
Use extension UINavigationController
Full code here:
4. Dismiss Keyboard and open picker/popup use text field
- This example to use two text field and one of the other for picker or popup, if this code
https://medium.com/@KaushElsewhere/how-to-dismiss-keyboard-in-a-view-controller-of-ios-3b1bfe973ad1
doesn't work
5. Integer rounding off (down) swift
- user this logic
- how to use
or you can use this metthod
https://stackoverflow.com/a/40951297/8366535
6. ScrollView can't scroll
ScrollView in swift 5.1 can't scroll, try this soluttion
- unchecklist content layoutt guides
7. Why UILabel get error "label Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
In condition to solve a problem is It is very common to define outlets as:
https://stackoverflow.com/questions/43404111/why-does-uilabel-text-result-in-fatal-error-unexpectedly-found-nil-while-unwra
8. Get row height size UITableViewCell
How to get the row height for each row in an UITableView in swift
// simple code use this
9. Tuples with Array
Here example use tuple with an array:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == currentRow {
if cellTapped == false {
cell.image.hidden = false
} else {
cell.image.hidden = true
}
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
let selectedRowIndex = indexPath
currentRow = selectedRowIndex.row
tableView.reloadData()
}
source: https://stackoverflow.com/a/34603224/8366535
2. SwiftUI Basic Note
- Parent view just have 10 limits to make child views
example:
Text("Hello World") // 0 . . . Text("Hello World") // 9- Alternative use form and section split that views. If using multiple forms it doesn't work.
example:
Form {
Section {
Text("SwiftUI")
Text("SwiftUI")
Text("SwiftUI")
}
Section {
Text("SwiftUI")
Text("SwiftUI")
Text("SwiftUI")
}
}
- NavigationView : to add navigation on the top controller
- .navigationBarTitle(Text("SwiftUI APP")) : for title navigation in swiftUI, displayMode
- displayMode: .large : for change title size (default is .large), .large is big use big font and .inline is normal font
- .colorScheme(.dark) : change theme .dark or .light mode theme
- .foregroundColor(.blue) : for text color in form
import SwiftUI
struct BasicContentView: View {
var body: some View {
NavigationView {
Form {
Section {
Text("SwiftUI")
Text("SwiftUI")
Text("SwiftUI")
Text("SwiftUI")
}
Section {
Text("Hi")
Text("My Name Is LF")
Text("I Know ")
Text("About SwiftUI")
Text("About SwiftUI")
}
}
.navigationBarTitle(Text("SwiftUI APP"), displayMode: .large)
.colorScheme(.dark) // For CHANGE DARK MODE / LIGHT MODE
.foregroundColor(.blue)
}
}
}
- Create a new variable must be use @State(property wrapper), if you did not use a that must be an error like this below
Left side of mutating operator isn't mutable: 'self' is immutable
- In SwiftUI Segmented Control create with picker
- Color is a View (Color.red), example :
.background(Color.red)
- every time you write some View. This means “one specific type that conforms to the View protocol(source: https://www.hackingwithswift.com/books/ios-swiftui/why-does-swiftui-use-some-view-for-its-view-type)
3. Custom leftBarButton can still swipe left
Use extension UINavigationController
extension UINavigationController {
open override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
}
}
Full code here:
import UIKit
class NavigateToAppsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
let backButton = UIButton(type: .system)
backButton.setImage(UIImage(named: "ic_Mystery"), for: .normal)
backButton.frame.size = CGSize(width: 28, height: 34)
backButton.addTarget(self, action: #selector(dismissSelf), for: .touchUpInside)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: backButton)]
}
@IBAction @objc func dismissSelf() {
UIView.animate(withDuration: 0.3) {
print("dismiss")
self.navigationController?.popViewController(animated: true)
}
}
}
extension UINavigationController {
open override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
}
}
4. Dismiss Keyboard and open picker/popup use text field
- This example to use two text field and one of the other for picker or popup, if this code
https://medium.com/@KaushElsewhere/how-to-dismiss-keyboard-in-a-view-controller-of-ios-3b1bfe973ad1
doesn't work
class ViewConrtoller: UIViewController {
@IBOutlet weak var textFieldOne: UITextField!
@IBOutlet weak var textFieldTwo: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: use editing did begin
@IBAction func handleTextFieldAsButton(_ sender: UITextField) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.textField2.resignFirstResponder()
print("open popup")
}
}
}
5. Integer rounding off (down) swift
- user this logic
// MARK: Digits add
func handleDigits(_ first: Int, _ count: Int) -> Int {
var digits: [Int] = []
digits.append(first)
for _ in 1 ..< count {
digits.append(0)
}
let intValue = digits.reduce(0) { $0 * 10 + $1 }
return intValue
}
- how to use
let numbers = "1234"
guard let _amount = numbers?.first else {
return false
}
let digitsAdd = handleDigits(Int("\(_amount)") ?? 0, numbers?.count ?? 0)
print(numbers, "-->", digitsAdd) // 1234 --> 1000
or you can use this metthod
let digitsAdd = handleDigits(Int("\(_amount)") ?? 0, numbers?.count ?? 0)
print(numbers, "-->", digitsAdd) // 1234 --> 1000
https://stackoverflow.com/a/40951297/8366535
6. ScrollView can't scroll
ScrollView in swift 5.1 can't scroll, try this soluttion
- unchecklist content layoutt guides
7. Why UILabel get error "label Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
In condition to solve a problem is It is very common to define outlets as:
@IBOutlet weak var label: UILabel!
...instead of the safer:
@IBOutlet weak var label: UILabel?
https://stackoverflow.com/a/43404173/8366535https://stackoverflow.com/questions/43404111/why-does-uilabel-text-result-in-fatal-error-unexpectedly-found-nil-while-unwra
8. Get row height size UITableViewCell
How to get the row height for each row in an UITableView in swift
var height: CGFloat = 0 for cell in tableView.visibleCells { height += cell.bounds.height // all row cell // or height = cell.bounds.height // for 1 row cell } print(height)link: https://stackoverflow.com/a/48107136/8366535
// simple code use this
let height = tableView.contentSize.height
tableHeight.constant = height
9. Tuples with Array
Here example use tuple with an array:
import UIKit
class ViewController: UIViewController {
var tuples = [(key: String, value: String)]()
override func viewDidLoad() {
super.viewDidLoad()
let some = "email=me@gmaa.com&int=20&Id=hqr123id"
let array = some.components(separatedBy: "&")
for i in array {
let arr = i.components(separatedBy: "=")
tuples.append((arr[0], arr[1]))
print(i.components(separatedBy: "="))
}
print("\n", tuples)
}
}
9 Notes In February 2020
Reviewed by Luffyselah
on
March 02, 2020
Rating: 5