Add a sub-view to a DetailView UIView in iPad

Posted by Elisabeth on Stack Overflow See other posts from Stack Overflow or by Elisabeth
Published on 2010-05-08T04:56:08Z Indexed on 2010/05/08 4:58 UTC
Read the original article Hit count: 724

I'm creating a split view controller app, the detail view has a segmented control in a navigation bar at the top. Clicking on a segment will add a new view to the detail view with the appropriate information on it (covering up the DetailViewController's default UIView).

I've created two new UIViews, corresponding to each segment, and I'm trying to add them to the view like this (in DetailViewController.m):

if (exerciseSegmentControl.selectedSegmentIndex == UISegmentedControlNoSegment) {
    NSLog(@"No segment selected");
}
UIView *viewToShow;
if (selectedView == 0 && exerciseSegmentControl.selectedSegmentIndex == 1) {
    viewToShow = exerciseSolutionView;
}
else {
    viewToShow = exerciseView;
}

[self.view addSubview:viewToShow];

I see the view appear, but it's in the wrong place, it is placed at the very top of the window, instead of below the navigation bar.

In IB, I've created instances of the views, and I've used the Attributes inspector to specify "Navigation Bar" for top bar, which sets the height of the view correctly. But the view is clearly being added too far up in the window - I see the view below it (the DetailViewController's UIView) peaking out at the bottom (I changed the background color so I know which view I'm seeing).

Any tips on how to get the subview I'm adding to get placed correctly in the window?

Thanks!

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about ipad