9 Notes in September Swift 5.1


Now is September  (wake me up when September ends  😋), this note for this month


1.  Set UIView height equal safe Area

self.titleViewHeight.constant = view.safeAreaInsets.top //top

self.yourViewHeight.constant = view.safeAreaInsets.left //left

self.yourViewHeight.constant = view.safeAreaInsets.right //right

self.yourViewHeight.constant = view.safeAreaInsets.bottom //bottom



2. Swipe UITabelViewCell Action (Editing Style)

If you search how to swipe cell in tableview action button here 3 option for you


- Delete Or Insert type editing style

   // first metode
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0, 3:
            if editingStyle == .delete {
                objects.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: .fade)
                
            } else if editingStyle == .insert {
                objects.append("71818")
            }
        default:
            break
        }
    }

- Custom editing style with text
    // second metode
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        switch indexPath.row {
        case 1, 3:
            let more = UITableViewRowAction(style: .normal, title: "More") { _, _ in
                print("more button tapped")
            }
            more.backgroundColor = .lightGray
            
            let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { _, _ in
                print("favorite button tapped")
            }
            favorite.backgroundColor = .orange
            
            let share = UITableViewRowAction(style: .normal, title: "Share") { _, _ in
                print("share button tapped")
            }
            share.backgroundColor = .blue
            
            return [more, favorite, share]
        default:
            
            let delete = UITableViewRowAction(style: .normal, title: "Delete") { _, _ in
                print("share button tapped")
            }
            delete.backgroundColor = .red
            
            return [delete]
        }
    }


- Custom editing style with Image


   // Third Metode
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->  UISwipeActionsConfiguration? {  // right
        let important = importantAction(at: indexPath)
        return UISwipeActionsConfiguration(actions: [important])
    }

    func importantAction(at indexPath: IndexPath) -> UIContextualAction {
        let action = UIContextualAction(style: .normal, title: "Important") { _, _, completion in
            completion(true)
        }
        action.image = UIImage(named: "ic_add")
        action.backgroundColor = #colorLiteral(red: 0.1294117647, green: 0.1294117647, blue: 0.1294117647, alpha: 1)
        return action
    }
    
    func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {  // left
        let important = importantAction(at: indexPath)
        return UISwipeActionsConfiguration(actions: [important])
    }

3. String? to NSString Fixing Error


if you have some error like this String?' is not convertible to 'NSString'; did you mean to use 'as!' to force downcast?  
  
// example 
// data name is type String?


let nstr = text.name as NSString // error in this line

//fixing

let nstr = "\(text.name ?? "")" as NSString

print(nstr)



4. Xcode Buildtime Error: 'Unable to load contents of file list: '…/Info.plist' (in target 'xxxx')

https://stackoverflow.com/questions/55011171/xcode-buildtime-error-unable-to-load-contents-of-file-list-info-plist


5. Error Change Apps

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: 
Error Domain=io.realm Code=1 "Provided schema version 2 is less than 
last set version 42." UserInfo={NSLocalizedDescription=Provided schema 
version 2 is less than last set version 42., Error Code=1}

Solution:

Just remove/delete the app from Simulator/Device and try again. This is happening because I changed the database scheme without doing a migration (we don't need to deal with it right now :-))https://github.com/RocketChat/Rocket.Chat.iOS/issues/104



6. error: Build input files cannot be found:

If you found bug "error: Build input files cannot be found:"  + "your file", its error is a missing file in your project or duplicate, can be solve in

https://stackoverflow.com/questions/52401856/problems-after-upgrading-to-xcode-10-build-input-file-cannot-be-found


https://stackoverflow.com/questions/52435202/build-input-file-cannot-be-found-swift-4-2-xcode-10-0

If you have this folder (Recovered Reference) in ur project you can delete file in this folder because file in this folder is missing.

 


7. Disable blinking cursor from UITextField in swift?


textField.tintColor = UIColor.clear
Or  change  your tint color same as background color

https://stackoverflow.com/a/53869445/8366535




8. Check PhotoLibrary Permission



func checkPhotoLibraryPermission()-> Bool{
        let status = PHPhotoLibrary.authorizationStatus()
        switch status {
        case .authorized:
            print("authorized")
            return true
            
        case .denied:
            print("denied") // it is denied
            return false
            
        case .notDetermined:
            print("notDetermined")
            return false
            
        case .restricted:
            print("restricted")
            return false

        }
        return false
    }


9. Changing Placeholder Text Color with Swift

 self.amountTextField.attributedPlaceholder = NSAttributedString(
        string: "Min. Rp 50.000", attributes: [
            NSAttributedString.Key.foregroundColor: UIColor.white
          ])

Set in XIB File :

placeholderLabel.textColor 



https://stackoverflow.com/a/26076202/8366535






2 comments: