Wednesday, January 30, 2019

How to check if bluetooth is enabled

viewDidLoad:

self.cbCentralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey : false])
self.cbCentralManager.delegate = self


handle:

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .poweredOn:
            btEnabled = true
            break
        case .poweredOff:
            btEnabled = false
            break
        case .resetting:
            break
        case .unauthorized:
            break
        case .unsupported:
            break
        case .unknown:
            break
        default:
            break
        }
    }




Protocol:

extension MyVC: CBCentralManagerDelegate {
}

Friday, January 25, 2019

How to initialize a ViewControl with arguments

class ViewController: UIViewController {

    var locationManager: CLLocationManager
    var userLocation: CLLocation?

    init(locationProvider: CLLocationManager = CLLocationManager()) {
        self.locationManager = locationProvider
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
       locationManager.delegate = self
    }

    func requestUserLocation() {
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
            locationManager.startUpdatingLocation()
        } else {
            locationManager.requestWhenInUseAuthorization()
        }
    }
}

Tuesday, January 22, 2019

How to get the tint color of your view, so text in a UILabel looks like a link

self.view.tintColor;

How to set the font size without changing the font itself

cell.textLabel?.font = UIFont.systemFont(ofSize: 15.0, weight: UIFont.Weight.regular)

cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 12.0, weight: UIFont.Weight.regular)

Wednesday, January 9, 2019

How to get the current orientation of the device without knowing it is portrait or landscape

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("landscape")
    } else {
        print("portrait")
    }
}


How to add refresh controller to table when drag down

1. initinalize a variable:

initialize
private let apRefreshControl = UIRefreshControl()

viewdidload:
// Add Refresh Control to Table View
self.apRefreshControl.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged)
self.tableView.refreshControl = apRefreshControl
// Add Refresh Control to Table View

if #available(iOS 10.0, *) {
    tableView.refreshControl = apRefreshControl
} else {
    tableView.addSubview(apRefreshControl)
}

//Configure Refresh Control
apRefreshControl.addTarget(self, action: #selector(refreshWeatherData(_:)), for: .valueChanged)


handler
@objc private func refreshWeatherData(_ sender: Any) {
    fetchData()
}
private func fetchData() {
    self.tableView.reloadData()
    self.apRefreshControl.endRefreshing()
}


How to dynamically get label height

extension UILabel {
    var optimalHeight : CGFloat {
        get
        {
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude))
            label.numberOfLines = 0
            label.lineBreakMode = NSLineBreakMode.byWordWrapping
            label.font = self.font
            label.text = self.text
            label.sizeToFit()
            return label.frame.height
        }
        
    }
}