Posts

Showing posts from February, 2023

UIButton title image padding not working iOS swift

Solution:  //   MARK: - button extension   UIButton   {       func   setTitleImagePadding ( value : CGF loat ) {           if   #available ( iOS   15.0, *) {               var   configuration =   UIButton . Configuration . plain ()               configuration. titlePadding   = value               configuration. imagePadding   = value               configuration. contentInsets   =   NSDirectionalEdgeInsets ( top : value,   leading : value,   bottom : value,   trailing : value)               self . configuration   = configuration           }   else   {               // Fallback on earlier versions               self . titleEdgeInsets   =   UIEdgeInsets ( top : 0,   left : value,   bottom : 0,   right : 0)               self . imageEdgeInsets   =   UIEdgeInsets ( top : 0,   left : value,   bottom : 0,   right : 0)           }                } You can use this extension for all the versions to set the padding

Create an App with Login Page iOS swift

Solution: import UIKit class LoginViewController : UIViewController {          let usernameTextField : UITextField = {         let textField = UITextField()         textField.translatesAutoresizingMaskIntoConstraints = false         textField.placeholder = "Username"         textField.borderStyle = .roundedRect         return textField     }()          let passwordTextField : UITextField = {         let textField = UITextField()         textField.translatesAutoresizingMaskIntoConstraints = false         textField.placeholder = "Password"         textField.borderStyle = .roundedRect         textField.isSecureTextEntry = true         return textField     }()          let loginButton : UIButton = {         let button = UIButton(type: .system)         button.translatesAutoresizingMaskIntoConstraints = false         button.setTitle( "Login" , for : .normal)         return button     }()          override func viewDidLoad () {         super . viewD

ColectionViewController Example iOS swift

Solution: The below code will explain you how to create collectionview in swift Code: import UIKit class MyViewController : UICollectionViewController {          let reuseIdentifier = "viewcell"     let items = [ "item 1" , "item 2" , "item 3" , "item 4" , "item 5" ]          override func viewDidLoad () {         super . viewDidLoad ()                  collectionView .backgroundColor = .white         collectionView .register(UICollectionViewCell. self , forCellWithReuseIdentifier: reuseIdentifier )     }          // MARK: UICollectionViewDataSource          override func collectionView ( _ collectionView: UICollectionView, numberOfItemsInSection section: Int ) -> Int {         return items .count     }          override func collectionView ( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {         let cell = collectionView.dequeueReusableCell(withReuse

TableView with RightSwipe options like Gmail

Solution: The below code can be used for rightswipe implementation in swift tableviewcell. func tableView ( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCell(withIdentifier: "Mycell" , for : indexPath) as ! CustomCell     let item = data[indexPath.row]          // Add swipe gesture recognizer to the cell     let swipeRight = UISwipeGestureRecognizer(target: self , action: #selector ( handleSwipe ))     swipeRight.direction = . right     cell.addGestureRecognizer(swipeRight)          // Configure the cell     cell.titleLabel.text = item.title     cell.descriptionLabel.text = item.description          return cell } @objc func handleSwipe ( gesture : UISwipeGestureRecognizer) {     if gesture.direction == . right {         // Get the index path of the cell that was swiped         let indexPath = tableView .indexPathForRow(at: gesture.location( in : tableView ))                  // Perfor

TabBar example iOS swift

Solution: Drag and drop the tabbarviewController to storyboard and add 2 viewControllers to that then proceed with the below code in appdelegate Code: import UIKit @UIApplicationMain class AppDelegate : UIResponder, UIApplicationDelegate {     var window : UIWindow?     func application ( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any ]?) -> Bool {         // Override point for customization after application launch.                  let tabBarController = window ?.rootViewController as ! UITabBarController         tabBarController.selectedIndex = 0                  return true     } }

Create signup page with username,password,email and confirm password using rxswift

  Solution : Using signUpViewModel import RxSwift import RxCocoa class SignupViewModel {          let username = BehaviorRelay< String >(value: "" )     let password = BehaviorRelay< String >(value: "" )     let email = BehaviorRelay< String >(value: "" )     let confirmPassword = BehaviorRelay< String >(value: "" )          let usernameValid : Observable< Bool >     let passwordValid : Observable< Bool >     let emailValid : Observable< Bool >     let confirmPasswordValid : Observable< Bool >     let signupEnabled : Observable< Bool >          init () {         usernameValid = username .asObservable().map { $0.count >= 10 }         passwordValid = password .asObservable().map { $0.count >= 8  }         emailValid = email .asObservable().map { $0.contains( "@" ) && $0.contains( "." ) }         confirmPasswordValid = Observable.combineLate

Print a scrollview as pdf in iOS swift

 Problem: I had a view that has scrollview with dynamic data like images and text.I want to print the entire view to export as pdf and print.How can i achieve that? Solution: You can convert the screen to data and write it into pdf then you can use printController to print easily.As like below Code: import UIKit import PDFGenerator class ViewController : UIViewController {     @IBAction func printAction ( _ sender: Any ) {         let printInfo = UIPrintInfo(dictionary: nil )           printInfo.jobName = newScroll .description         printInfo.outputType = .general       let path = self . newScroll . exportAsPdfFromViews ()         let printController = UIPrintInteractionController.shared         printController.printInfo = printInfo           printController.printingItem = NSURL(fileURLWithPath:path)         printController.present(animated: true )     }          var newScroll = UIScrollView()     override func viewDidLoad () {         super . viewDidLoad ()         new