Post

Replies

Boosts

Views

Activity

Reply to How to distinguish textFields of a prototype cell in tableView?
Here is how I tried to achieve my needs: func textFieldDidChangeSelection(_ textField: UITextField) { let tapLocation = textField.convert(textField.bounds.origin, to: tableView) guard let pickedCurrencyIndexPath = tableView.indexPathForRow(at: tapLocation) else { return } //Append only if array don't have the IndexPath already if !indexPathsArray.contains(where: {$0 == pickedCurrencyIndexPath}) { indexPathsArray.append(pickedCurrencyIndexPath) } //I need to compare only 2 last IndexPaths, so delete the most old at position 0 in array if indexPathsArray.count > 2 { indexPathsArray.remove(at: 0) } for indexPath in indexPathsArray { if indexPath.last != indexPath.first { //user clicked on a new textfield,clear all so he can type a new number numberFromTextField = 0 textField.placeholder = "0" textField.text = "" } } print(indexPathsArray) } With this code I almost achieved what I need except when I click on a new textField I can't type anything... GIF: cln.sh/sGNlBE
Topic: UI Frameworks SubTopic: UIKit Tags:
Feb ’22
Reply to How to enable UIScrollView scroll on a nested UITableView while dragging a cell?
What is the UILabel Here on GIF: https ://cln.sh/j45g83 ? It's a grey text just below the searchBar and before the first cell. Take a look I made a new gif for you: https://cln.sh/ajHYYt When do you want the label to move ? When you scroll the tableView ? Yes, when I drag tableView, label should follow If so, why not just put this label in HeaderView of the table ? Can I put it just like it shows now? (right aligned, the same color and size) Can you suggest how can I move it correctly? Having a tableView (which is a scrollView) inside another scrollView is complex to handle. Basically I can't scroll my tableView. It has a fixed size since I have the exact amount of cells and it's not possible to delete or add to it. Just reorder. The only reason I put it all inside a scrollView is to be able to interact with a Navigation Bar Large Title: if I put a label before TableView without scrollView, then when I drag tableView up - the NavBar won't animate (going up), it will be like fixed since label will interfere...
Topic: UI Frameworks SubTopic: UIKit Tags:
Feb ’22
Reply to UIGestureRecognizer works outside of UIView borders instead of inside
Here is how I was able to achieve my needs: Switch popupView.isUserInteractionEnabled = false to start swipe recognises not only outside the view change my didSwipe method onto this: @objc private func didSwipe(_ sender:UISwipeGestureRecognizer) { guard let window = UIApplication.shared.windows.first(where: {$0.isKeyWindow}) else { return } let tappedArea = sender.location(in: popupView) let popupFrame = CGRect(x: tappedArea.x, y: tappedArea.y, width: popupView.frame.width, height: popupView.frame.height) if sender.direction == .up, window.frame.contains(popupFrame) { UIView.animate(withDuration: 0.15, delay: 0.0, options: .curveLinear) { self.popupView.center.y -= 50 } completion: { _ in self.popupView.removeFromSuperview() } self.isRemovedBySwipe = true } } Now I am able to swipe out the popup only when swipe inside its borders and ignore swipes outside of it.
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’22
Reply to How to format numbers in a textField with math equation string?
Here is how I solved my problem: func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.maximumFractionDigits = 2 formatter.locale = .current formatter.roundingMode = .down //set of possible math operations let symbolsSet = Set(["+","-","x","/"]) let numberString = textField.text ?? "" guard let range = Range(range, in: numberString) else { return false } let updatedString = numberString.replacingCharacters(in: range, with: string) let correctDecimalString = updatedString.replacingOccurrences(of: formatter.decimalSeparator, with: ".") let completeString = correctDecimalString.replacingOccurrences(of: formatter.groupingSeparator, with: "") //receive math symbol user typed let symbol = symbolsSet.filter(completeString.contains).first ?? "" //receive number of symbols in a String. If user wants to type more than one math symbol - do not insert let amountOfSymbols = completeString.filter({String($0) == symbol}).count if amountOfSymbols > 1 { return false } //receive numbers typed by user let numbersArray = completeString.components(separatedBy: symbol) //check for each number - if user wants to type more than one decimal sign - do not insert for number in numbersArray { let amountOfDecimalSigns = number.filter({$0 == "."}).count if amountOfDecimalSigns > 1 { return false } } guard let firstNumber = Double(String(numbersArray.first ?? "0")) else { return true } guard let secondNumber = Double(String(numbersArray.last ?? "0")) else { return true } let firstFormattedNumber = formatter.string(for: firstNumber) ?? "" let secondFormattedNumber = formatter.string(for: secondNumber) ?? "" // if user typed math symbol - show 2 numbers and math symbol, if not - show just first typed number textField.text = completeString.contains(symbol) ? "\(firstFormattedNumber)\(symbol)\(secondFormattedNumber)" : "\(firstFormattedNumber)" return string == formatter.decimalSeparator }
Topic: UI Frameworks SubTopic: UIKit Tags:
Jul ’22
Reply to How to replace a specific character in a string inside a UITextField?
@Claude31 The closer I can get is to use the code: var completeString = correctDecimalString.replacingOccurrences(of: formatter.decimalSeparator, with: ".") if amountOfSymbols > 1 {   let newSymbol = completeString.last?.description ?? ""   completeString.removeAll { symbolsSet.contains(String($0))}     textField.text = completeString+newSymbol     return false } But then If I'll add math symbol again it will change the first number which I want to avoid: https://share.cleanshot.com/tmFiAr
Topic: UI Frameworks SubTopic: UIKit Tags:
Jul ’22
Reply to How to replace a specific character in a string inside a UITextField?
@Claude31 Here is what I get in console: Put print statements before check on how much math symbols string has: func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { //all possible math operation symbols user can add let symbolsSet = Set(["+","-","x","/"]) var amountOfSymbols = 0 let numberString = textField.text ?? "" guard let range = Range(range, in: numberString) else { return false } let updatedString = numberString.replacingCharacters(in: range, with: string) let correctDecimalString = updatedString.replacingOccurrences(of: formatter.groupingSeparator, with: "") let completeString = correctDecimalString.replacingOccurrences(of: formatter.decimalSeparator, with: ".") //current math symbol user add let symbol = symbolsSet.filter(completeString.contains).last ?? "" //if user add math symbol to an empty string - do not insert if string == symbol, numberString.count == 0 { return false }  print("symbol", symbol)  print("string", string)  print("updatedString", updatedString)  print("correctDecimalString", correctDecimalString) print("completeString", completeString) //count how much math symbols string has. If more that one - do not insert, string can have only one completeString.forEach { character in if symbolsSet.contains(String(character)) { amountOfSymbols += 1 } } if amountOfSymbols > 1 { return false } } Here is the console output after each character I type: Press 2 symbol  string 2 updatedString 2 correctDecimalString 2 completeString 2 Press + symbol + string + updatedString 2+ correctDecimalString 2+ completeString 2+ Press - symbol + string - updatedString 2+- correctDecimalString 2+- completeString 2+- 2.Put print statements after check on how much math symbols string has: if amountOfSymbols > 1 { return false }   print("symbol", symbol)   print("string", string)   print("updatedString", updatedString)   print("correctDecimalString", correctDecimalString)   print("completeString", completeString) Here is the console output after each character I type: Press 2 symbol  string 2 updatedString 2 correctDecimalString 2 completeString 2 Press + symbol + string + updatedString 2+ correctDecimalString 2+ completeString 2+ Press - Nothing is printed
Topic: UI Frameworks SubTopic: UIKit Tags:
Jul ’22