9 Notes for me in July Swift 4.2

1. How to disable right Bar Button Item

This ex :
//for disable 
 self.navigationItem.rightBarButtonItem?.isEnabled = true

//for enable
self.navigationItem.rightBarButtonItem?.isEnabled = false



2. Specific popToViewController Back Button


//---- In order to get to some specific view controller in the stack you use: 

self.navigationController?.popToViewController(PayAndPurchaseViewController(), animated: true)

//--- if error use that, you can try a below

self.navigationController?.viewControllers.forEach({ (vc) 
  if vc is PayAndPurchaseViewController {
   self.navigationController?.popToViewController(vc, animated: true)}
})

3. How use didHighlightItemAt And didUnhighlightItemAt

this example how to use change background color if selected uicollectionviewcell.
you must include UICollectionviewDelegateFlowLayout in your viewController .


// change background color when user touches cell
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { let cell = collectionView.cellForItem(at: indexPath) as? CustomCell cell?.backgroundColor = UIColor.gray } // change background color back when user releases touch func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) { let cell = collectionView.cellForItem(at: indexPath) as? CustomCell cell?.backgroundColor = UIColor.white }

4.  Shadow UIButton Swift 4.2

 // Shadow and Radius

yourButton.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
yourButton.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
yourButton.layer.shadowOpacity = 1.0
yourButton.layer.shadowRadius = 0.0
yourButton.layer.masksToBounds = false
yourButton.layer.cornerRadius = 4.0

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

5. Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"

Don't change format data example: "from do.wav to do.mp3"

your audio files are not recognized by Xcode / OS or corrupted. To verify this step, you can select one of your audio file and try to playing it inside Xcode



6. Piano Audio effect "DO" Swift 4.2

import UIKit
import AVFoundation

class TapBoxMusicViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    @IBOutlet weak var collectionView: UICollectionView!
    var audio = AVAudioPlayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
        
    }
    
    func playSound() {
        guard let url = Bundle.main.url(forResource: "do-octave", withExtension: "wav") else { return }
        do
        {
            try AVAudioSession.sharedInstance().setActive(true)
            audio = try AVAudioPlayer(contentsOf: url)
            audio.play()
        } catch let error {
            print(error.localizedDescription)
        }
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 7
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
        cell.layer.backgroundColor = #colorLiteral(red: 0.5728681085, green: 0.9516503212, blue: 1, alpha: 1)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
       self.playSound()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let collectionViewSize = (collectionView.frame.size.height / 7) - 7
        
        return CGSize(width: collectionView.frame.size.width, height: collectionViewSize)
    }
    

}

7. UIView Over keyboard swift 4

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

 let customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height - 300, width: self.view.frame.size.width, height: 300))
 customView.backgroundColor = UIColor.red
 customView.layer.zPosition = CGFloat(Float.greatestFiniteMagnitude)
 UIApplication.shared.windows.last?.addSubview(customView)


8. CustomSegmentControl




9. CustomSegmentControll with collectionview and image

in viewController

import UIKit

struct ICustomTabbar {
    var active: Bool
    var list: Int
}

class CustomSegmentControllerViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    @IBOutlet weak var collectionView: UICollectionView!
    let tabbarList = [0, 1, 2, 3, 4, 5, 6]
    let tabbarBool = [true, false, false, false, false, false, false]
    
    var listActive = [ICustomTabbar]()
    var int21: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "CutomTabbarCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cellId")
        
        for (list, bool) in zip(tabbarList, tabbarBool) {
            listActive.append(ICustomTabbar(active: bool, list: list))
        }
    }
    
    
    // datasource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listActive.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CutomTabbarCollectionViewCell
        cell.tabbar = listActive[indexPath.item]
        if listActive[indexPath.item].list == int21 {
            cell.inBackColor = #colorLiteral(red: 0.2275405646, green: 0.6914991549, blue: 1, alpha: 1)
        } else {
            cell.inBackColor = #colorLiteral(red: 0.5973196626, green: 0.7864208817, blue: 0.9157722592, alpha: 1)
        }
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? CutomTabbarCollectionViewCell
        if indexPath.item == listActive[indexPath.item].list {
            cell?.inBackColor = #colorLiteral(red: 0.2275405646, green: 0.6914991549, blue: 1, alpha: 1)
            int21 = indexPath.item
        } else {
            cell?.inBackColor = #colorLiteral(red: 0.5973196626, green: 0.7864208817, blue: 0.9157722592, alpha: 1)
        }
    }

    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? CutomTabbarCollectionViewCell
        if indexPath.item != int21 {
            cell?.inBackColor = #colorLiteral(red: 0.5973196626, green: 0.7864208817, blue: 0.9157722592, alpha: 1)
        } else {
            cell?.inBackColor = #colorLiteral(red: 0.2275405646, green: 0.6914991549, blue: 1, alpha: 1)
        }
        collectionView.reloadData()
    }
    
    // size for Item
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (self.collectionView.frame.size.width) / 7
        let height = self.collectionView.frame.size.height
        return CGSize(width: width, height: height)
    }
}


in collectionviewCell


 //
//  CutomTabbarCollectionViewCell.swift
//  100DayOfCode


import UIKit

class CutomTabbarCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imgView: UIImageView!
    
    var tabbar: ICustomTabbar? {
        didSet {
            switch self.tabbar?.list {
            case 0:
                self.imgView.image = #imageLiteral(resourceName: "Bw48")
            case 1:
                self.imgView.image = #imageLiteral(resourceName: "Bw48")
            case 2:
                self.imgView.image = #imageLiteral(resourceName: "Bw48")
            case 3:
                self.imgView.image = #imageLiteral(resourceName: "Bw48")
            case 4:
                self.imgView.image = #imageLiteral(resourceName: "Bw48")
            case 5:
                self.imgView.image = #imageLiteral(resourceName: "Fc47")
            case 6:
                self.imgView.image = #imageLiteral(resourceName: "Fc47")
            default:
                print("spspp")
                break
            }
        }
    }
    
    var inBackColor: UIColor? {
        didSet {
            guard let _in = inBackColor else {return}
            self.inBack(_in)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    func inBack(_ inBackColor: UIColor) {
        layer.backgroundColor = inBackColor.cgColor
    }
}



No comments: