Posts

Showing posts from August, 2023

Add toolbar with done and cancell for datepicker iOS swift

Problem:  Want to create a date picker for textfield while tap show date picker with done and cancel buttons with action. Solution:                      let toolBar = UIToolbar ()                     toolBar. sizeToFit ()                     let doneBtn = UIBarButtonItem ( barButtonSystemItem : . done , target : nil , action : #selector ( doneAction ))                     let cancelBtn = UIBarButtonItem ( barButtonSystemItem : . cancel , target : nil , action : #selector ( cancelAction ))                     let spaceBtn = UIBarButtonItem ( barButtonSystemItem : . flexibleSpace , target : nil , action : nil )                      myTF . inputAccessoryView = toolBar                     myTF . inputView = datePicker      // MARK: - Done action     @objc func doneAction () {          self . view . endEditing ( true ) }      // MARK: - cancel action     @objc func cancelAction () {         self . view . endEditing ( true )     }

open document picker and upload iOS swift

 Solution: var  selectedDocPath:  URL! extension docVC : UIDocumentPickerDelegate {     func documentPicker ( _ controller: UIDocumentPickerViewController , didPickDocumentsAt urls: [ URL ]) {         // Handle the selected PDF file here         let selectedURL = urls. first         selectedDocPath = selectedURL         uploadDoc ()         // Process the selected PDF file     }          func documentPickerWasCancelled ( _ controller: UIDocumentPickerViewController ) {         // Handle cancellation     } } Use  selectedDocPath to upload the file to server

RXswift button action iOS swift

 Solution:                  cell. btn . rx . tap . asDriver ()                     . drive ( onNext : { [ weak self ] in                      }). disposed ( by : cell. disposebag )

RXSwift switch action iOS

 Solution:              cell.my Switch                 . rx                 . controlEvent (. valueChanged )                 . withLatestFrom (cell.my Switch . rx . value )                 . subscribe ( onNext : { bool in                     // this is the value of mySwitch                     let isON  =  cell.my Switch . isOn                  })                 . disposed ( by : cell. disposebag )

Apply gradient to UIButton iOS swift

 Solution: extension UIButton {     func applyGradient ( colors : [ CGColor ]) {         let gradientLayer = CAGradientLayer ()         gradientLayer. colors = colors         gradientLayer. startPoint = CGPoint ( x : 0.5 , y : 0 )         gradientLayer. endPoint = CGPoint ( x : 0.5 , y : 1 )         gradientLayer. frame = self . bounds         self . layer . insertSublayer (gradientLayer, at : 0 )     }      } extension UIButton {     func setBlackGrdient () {         self . applyGradient ( colors : [ UIColor (). colorWithHexString ( hexString : "#252525" ). cgColor , UIColor (). colorWithHexString ( hexString : "#4e4e4e" ). cgColor ])     }          func setWhiteGrdient () {         self . applyGradient ( colors : [ UIColor (). colorWithHexString ( hexString : "#f4f4f4" ). cgColor , UIColor (). colorWithHexString ( hexString : "#b6b6b6" ). cgColor ])     } } The above function we can create button with black gradient using  setBlackG

Change the status bar color in iOS swift

 Solution: extension UIViewController {      func statusBarColorUpdate () {                  if #available ( iOS 13.0 , *) {                 let window = UIApplication . shared . windows . first                 let topPadding = window?. safeAreaInsets . top                 let statusBar = UIView ( frame : CGRect ( x : 0 , y : 0 , width : UIScreen . main . bounds . size . width , height : topPadding ?? 0.0 ))                 statusBar. backgroundColor = UIColor.red                 UIApplication . shared . windows . filter {$0. isKeyWindow }. first ?. addSubview (statusBar)         } else {             let statusBarView = UIView ( frame : UIApplication . shared . statusBarFrame )             statusBarView. backgroundColor =  UIColor.red   // Set your desired color here             view . addSubview (statusBarView)                          // Update the layout to account for the status bar             view . layoutIfNeeded ()         }      } } Add the above function as extens

Get the age from birth date iOS swift

 Solution: The below function will be used for calculate the age based on the birth date iOS swift. extension Date {     func  find Age () -> String {         let now = Date ()         let birthday: Date = self         let calendar = Calendar . current                  let ageComponents = calendar. dateComponents ([. year ], from : birthday, to : now)         let age = ageComponents. year !         return String ( describing : age)     } }

Add asterisk symbol to label iOS swift

 Solution:   Call the below function to label to set the asterisk symbol.      func addRequired () {         let passwordAttriburedString = NSMutableAttributedString ( string : self . text ?? "" )         let asterix = NSAttributedString ( string : " *" , attributes : [. foregroundColor : UIColor . red ])         passwordAttriburedString. append (asterix)         self . attributedText = passwordAttriburedString     } }

UITextfield set padding on left and right iOS swift

 Solution: You can use the below extension function to set the padding for textfield in both right and left side. extension UITextField {     func setPadding () {         self . setLeftPaddingPoints ()         self . setRightPaddingPoints ()     }     func setLeftPaddingPoints ( _ amount: CGFloat = 5 ){         let paddingView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : amount, height : self . frame . size . height ))         self . leftView = paddingView         self . leftViewMode = . always     }     func setRightPaddingPoints ( _ amount: CGFloat = 5 ) {         let paddingView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : amount, height : self . frame . size . height ))         self . rightView = paddingView         self . rightViewMode = . always     } }

Convert boolean to string and Int extension function iOS swift

 Solution: extension Bool {     var toString : String ! {         String ( describing : self )     }     var toInt : Int ! {         return self == true ? 1 : 0     } } You can directly call as like below let isDone:Bool = false then call to string as isDone. toString() then call to Int as isDone. toInt ()