Oct 23, 2012

Top iOS programming bloopers for this week

Posted Oct 23, 2012

These are my top iOS bloopers for this week (and corresponding fixes):

  1. For proper spacing of Toolbar Items in iOS you can use

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    //this will center-align myButton
    [self setToolbarItems:[NSArray arrayWithObjects: flexibleSpace, myButton, flexibleSpace, nil]];

    or you can also use

    UIBarButtonSystemItemFixedSpace system item and set the width.
  2. 2nd or Bottom Navigation Toolbar in a popover (UIPopoverController)

    It is actually the toolbar item of the navigation controller.

    Assuming you added a navigation controller in a popover for the title,
    using initWithContentViewController: [UINavigationController]

    To show bottom toolbar, just set the navigation controller's toolbarHidden to NO.
  3. When changing the width of a UIBarButton, I often try to find the frame, but it's not there. Apparently, just set it directly using setWidth method.
  4. Change Navigation Controller's Back Button Title or Text
    Just accessing the backBarButtonItem will not work, you have to set the button entirely.
     self.navigationItem.backBarButtonItem =
          [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                   style: UIBarButtonItemStyleBordered
                   action:nil] autorelease];
  5. UIButton is not showing , also related topic: UIBarButtonItem not showing UIButton custom view

    [btn setBackgroundImage:image forState:UIControlStateNormal]
    instead of
    btn.imageView.image = .. //ugh

    The same way if title is not showing, use:
    [btn setTitle:itemTitle forState:UIControlStateNormal];
    instead of
    btn.titleLabel.text = ..

No comments:

Post a Comment