Posts

awakeFromNib, layoutSubviews, dequeueReusableCellWithIdentifier and what happens in what order

If you have customized code in your UITableViewCell subclass like myself, you usually have someIf you have customized code in your UITableViewCell subclass like myself, you usually have some startup code for the cell, usually setting some constraints, or colors of text, default placeholder, etc. It generally doesn't matter whether this code goes in awakeFromNib or layoutSubviews, as long as what you do after dequeueReusableCellWithIdentifier doesn't depend on logic in either of these startup methods.In the case where it does, you need to know which one happens in which order, in my case I'm using a custom UITextView that has placeholder text, via a nice blog post on how to do that:https://grokswift.com/uitextview-placeholder/The issue was that when opening a form to fill out for a new document the text is empty, so you need placeholder text. On top of this I have a label that needs to show (alpha = 1) when text from the user is inputted. When no text exists in the TextV…

Draw a red border around your UIView for debugging purposes

Image
A lot of the times, you're dealing with UITextViews and UITextFields that have the sameA lot of the times, you're dealing with UITextViews and UITextFields that have the same white background and border as the superviews they sit on top of. It's helpful every now and then to make sure they're obeying the constraints you want them to have. In my case, I made a self-contained UITextView that has a placeholder functionality but the textView wasn't constraining to the size of its container view.DOH!Ahh... needed better constraints:func applyConstraintsToTextView() { if let validTextView = self.placeholderTextView { let viewsDictionary = ["textView":validTextView] self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[textView]|", options: [], metrics: nil, views: viewsDictionary)) self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[textView]|", options: [], metrics: nil, v…

Quickly add a simple UIActivityIndicator to your screen

Sometimes you need to quickly throw a progress indicator over your view while something processesSometimes you need to quickly throw a progress indicator over your view while something processes (hopefully in the background).I've found this code to be useful for that situation. Apply to your needs.UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:self.view.frame]; activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; activityIndicatorView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]; [self.view addSubview:activityIndicatorView]; [activityIndicatorView startAnimating]; [self.presentingViewController dismissViewControllerAnimated:YES completion:^{ dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul); dispatch_async(queue, ^{ // do your processing in the background here dispatch_sync(dispatch_get_main_queue(), ^{…

Finder shortcut to open a directory path

Image
I find myself using this a lot after a co-worker showed me. Basically, if you come across a directory path you need to get to, like to drop files into a folder or delete file, whatever, instead of manually clicking each directory to get to the destination, you can copy and paste the path into finder via

   ⌘ + Shift + G

Modularize your UIImagePickerController usage in Swift

When you run across quick tutorials or answers on using UIImagePickerController the majority have you When you run across quick tutorials or answers on using UIImagePickerController the majority have you simply instantiating, containing, delegating and using it inside your UIViewController. As with most coding examples this is for demonstration purposes only, you should always, always abstract out functionality into reusable pieces in any application. The most obvious reason is that if one view controller needs the image picking functionalities, it's a safe bet that another will at some point. If not, well you'll probably run across another need for it in another application, much easier to strip out the code if it's self-containined in one class.
The history of this code has been in Objective-C and it's served me well in more than one app, here I'm converting and using it in Swift for the first time in an app I'm currently building.
To start, create a c…

Prefer Swift's for-in loop over traditional C for loop

I'm sure most of us started our first programming adventures in C. How many timesI'm sure most of us started our first programming adventures in C.How many times have you writtenfor (int i = 0; i < blah; i++) or something similar in countless methods, functions, etc.?In C, C++, Javascript, Java, it's ubiquitous.Then I came upon the most glorious programming language ever created, Rubyfor index in 0..5 puts "Value of local variable is #{i}" end This is obviously equivalent to:for (int index = 0; i <= 5; i++) The ease of reading should be obvious in Ruby's typical verbosity. Not that any programmer worth his/her salt can't immediately decipher a traditional for loop.The idea though is to flow code in a way we read natural language.So it was no suprise that Swift adopted this for-in loop pattern, and in all cases you should prefer to use this format over the C for loop.Here's Swift's for-infor index in 0...5 { print("current in…

Using Xamarin Android Player with Android Studio

Image
Android Studio and it's accompanying emulator have actually improved considerably. I'd even say the emulatorAndroid Studio and its accompanying emulator have actually improved considerably. I'd even say the emulator that ships now is perfectly sufficient to use, although there's obviously the better alternative(s) like GenyMotion.Having said that, the Xamarin Android Player is probably the best out there for a free fast booting emulator.First install the XAP, no need to install Xamarin Studio.Launch Android Studio, have your project ready to launch on an emulator.Here's thing, the running XAP (after you create a device and press play) will not show up in Android Studios list of devices in the AVD. Don't fret, as it won't matter.When you finally push the run button you will get a screen like this with an option to now launch on the XAP:Now you can use the XAP for your project :)