8
votes

I have upgraded my project from iOS 6 to iOS 7 but there seems a little problem. The status bar and a tool bar is overriding and very close to each other. The tool bar was earlier added by manually dragging it in the storyboard. This is how its showing up:

toolbar

I have seen a few questions that are suggesting to use "positionForBar:" and "- (UIBarPosition)positionForBar:(id)bar" but i don't know exactly how to use them, a little explanation and easy way to do it might help. Thanks!

UPDATE: Following is some code that also needs a fix. It was working fine earlier but since the detailviewcontroller (WebViewController) is now embeded inside a navigation controller, the below code is causing an exception. Looks like i need to modify the first line in this method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        WebViewController *wvc = [self.navigationController.parentViewController childViewControllers][1];
        RSSItem *entry = [[channel items] objectAtIndex:[indexPath row]];
        wvc.title = entry.title;
        wvc.urlString = entry.link;   
}
6

6 Answers

2
votes

If your setup is a split view like setup with two container views, you should be able to do this. When you set up the container views, drag the top up until you see the dotted blue line that indicates the top is at the bottom of the status bar. Do this with both container views. Add the tool bar to the embedded controller (not the container view), pinned to the top of that controller's view. With the left view being embedded in a navigation controller, my screen looked like this:

enter image description here

6
votes
_toolBar.delegate = self;

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
    CGRect frame = _toolBar.frame;
    frame.origin = CGPointMake(0, [UIApplication sharedApplication].statusBarFrame.size.height);
    _toolBar.frame = frame;

    return UIBarPositionTopAttached;
}

portrait bar

landscape bar

4
votes

Your view's top constraint should no longer be top space to superview, but rather top space top top layout guide.

You can do this by moving the top space down beneath the status bar and then using the constraint menu to add the constraint to the nearest neighbor, which should now be the top layout guide, or you can do it as describe in this link on apple's developer library.

3
votes

Remember that you shouldn't just move things down in IB for two reasons:

  1. Not compatible with iOS 6
  2. Won't extend the top bar background effect under the status bar

What not to do...

So, if you want to iOS6 & iOS7 compatibility, you could add a conditional for objects that require customization in ViewDidLoad. Caveat being this is a last case scenario - always try to remedy it with autolayout / IB first:

#import <Availability.h>

#ifdef __IPHONE_7_0

        CGRect barFrame = topBar.frame;
        barFrame.origin = CGPointMake(0, [UIApplication sharedApplication].statusBarFrame.size.height);
        [topBar setFrame:barFrame];

        // move other stuff around too

    #endif

And set your bar delegate much like Luniz above,override positionForBar to .

topBar.delegate = self;

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {

    return UIBarPositionTopAttached;
}
1
votes

I was trying to do pretty much the same thing, but I didn't like the white status bar of the accepted answer. I managed to make the toolbar placed underneath the status bar so that it has a consistent color to the Navigation Bar that is next to it in the Master view.

My solution was to make the Toolbar 64 points tall and set the Topspace to the superview to 0.

When doing this you have to make sure you are not creating the constraint to the Top Space Layout Guide. I was able to get that done in Interface Builder by setting the Frame to 0,0 with a Height of 64. Then I used the Pin dialog off of the floating tool menu. I used zero with the top I beam to create the constraint.

The bottom edges match in the Master and Detail and the coloring is consistent. The controls in my toolbar ended up uncrowded.

-1
votes

Swift 3

Connect your bar delegate and conform to UIToolbarDelegate. Then add this delegate method:

func position(for bar: UIBarPositioning) -> UIBarPosition {
    return UIBarPosition.topAttached
}

because now

enum UIBarPosition : Int {
    case any
    case bottom
    case top
    case topAttached
}