Skip to content

Instantly share code, notes, and snippets.

@rickyngan
rickyngan / ViewController.swift
Last active May 25, 2018 19:25
LoginButton enable when both username and password textfields are filled.
loginUserNameTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
loginUserPasswordTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
loginButton.enabled = loginUserNameTextField.text.length && loginUserPasswordTextField.text.length
}
let labelHeight = self.addressLabel.intrinsicContentSize().height
self.mapView.padding = UIEdgeInsets(top: self.topLayoutGuide.length, left: 0,
bottom: labelHeight, right: 0)
UIView.animateWithDuration(0.25) {
//2
self.pinImageVerticalConstraint.constant = ((labelHeight - self.topLayoutGuide.length) * 0.5)
self.view.layoutIfNeeded()
}
extension UIColor {
convenience init(rgb: UInt) {
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
@rickyngan
rickyngan / dateFormatter.swift
Created September 21, 2016 06:19 — forked from m1entus/dateFormatter.swift
Swift NSDateFormatter singleton
class var userDateFormatter : NSDateFormatter {
struct Static {
static let instance: NSDateFormatter = {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
return dateFormatter
}()
}
return Static.instance
}
@rickyngan
rickyngan / gist:b2ce9b88594e1f88ac1599d250803c1f
Created September 28, 2016 07:22
UISearchBar extension
import UIKit
extension UISearchBar {
var textField: UITextField? {
return self.value(forKey: "_searchField") as? UITextField
}
}
import UIKit
public extension UIView {
@available(iOS 10.0, *)
public func renderToImage(afterScreenUpdates: Bool = false) -> UIImage {
let rendererFormat = UIGraphicsImageRendererFormat.default()
rendererFormat.opaque = isOpaque
let renderer = UIGraphicsImageRenderer(size: bounds.size, format: rendererFormat)
let snapshotImage = renderer.image { _ in
@rickyngan
rickyngan / main.dart
Last active October 7, 2020 14:04
2020 Jun 13 HoC HK Flutter: 8_bottom_navigation_bar
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Flutter App',
home: Home(),
@rickyngan
rickyngan / main.dart
Last active June 11, 2020 07:30
2020 Jun 13 HoC HK Flutter: 7_route_navigator
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
@rickyngan
rickyngan / main.dart
Created June 11, 2020 07:39
2020 Jun 13 HoC HK Flutter: 6_row_column
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
@rickyngan
rickyngan / main.dart
Last active June 12, 2020 14:16
2020 Jun 13 HoC HK Flutter: 5_statefulwidget
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {