Small Things

Random, hard to remember things that have been time sucks for me due to inscrutable errors;

trying to manipulate/animate ui objects with frame info when I am using autolayout.

Remember - when coding in Xcode, you are almost certainly using Autolayout by default, so when you are moving things around in a view, you might be thinking of the frames in terms of position and widths/heights, but the compiler and runtime is largely "thinking" of these things in terms of constraints. If you don't have this in mind, you will get seemingly bizarre errors. Take this simple example of what ignoring auto layout looks like:

  1. Create a single view Xcode project.
  2. Add a button to the view. I put it at 100, 100.
  3. Drag an IBOutlet and an IBAction into the .h file of the viewController.
  4. Add the following code to the IBAction:
    [UIView animateWithDuration:1 animations:^{
        self.testButton.frame = CGRectMake(200, 200, 100, 100);

  1. When you press the button the button will immediately appear at the new location and then animate back to its original position. WHAT?!?! Basically, it can't carry out the instructions because they are ignoring the constraints that the system has auto-created.
  2. If you want to see that the animation is indeed correctly written if we were ignoring auto-layout, go to the storyboard, select the viewController icon in the left storyboard navigator, then select the "file inspector" (page looking icon) in the right navigator.
  3. Uncheck "use auto layout" and then run. It will animate correctly.
  4. Use auto layout. It will make things easier and is the future of Xcode programming. But the point of this exercise is to understand what it looks like when you have messed it up.

Dismissing a keyboard

  1. set viewController (usually self) as the delegate to any view that is going to receive input from the keyboard.
  2. this code: >

    • (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }

Creating Categories

  • ⌘-n "new category"
  • add the method in question to .m and .h files of the category files
  • open app-Prefix.pch
  • add #import "Class+category" to the header of any files using the new methods
  • NOTE: make sure new method names have 3 letter prefix to prevent accidental overlap