UITableView behaves differently in two identical tabs views

I created a simple project that demonstrates this strange behavior. I created all of this in the interface builder without touching the code so that it is easy for you to reproduce. StoryBoard


The start page is embedded in a navigation controller containing only one button, and when clicked, the screen will be directed to the tab views (with a navigation bar on top).
When the program starts up, two strange things happen.

1) The table view tab2 is under the navigation bar where when item1 is displayed correctly.
correct versus incorrect
As you can see, although the view of the two tables is placed in the same position when the program starts, the second even hides the search string due to misalignment.


2) When clicking on the search bar at tab 1, the navigation bar is not hidden as default behavior (where the navigation bar and search bar are hidden).
Since the navbar is semi-transparent, I can see the Cancel button below the navbar. (Anyone with a good eye can see the blue tint in the upper right corner of the screen.) This means the search bar is going to the right place, but the navbar just doesn't disappear.
incorrct again
Also note that the table view does not slide with the search bar (again, by default) when the search bar is active.

PS: I am using XCode 6. Also I tried to add constraints to tableview controllers but not cubes.
Any ideas on this? Thanks in advance.

+3


source to share


2 answers


As Logixilog suggested, you can put a navigation controller for each tab as the first tab controller. If you don't want to do this, you can add the following code to viewDidLoad from the view controller of the second item.

CGFloat shiftDist = CGRectGetHeight(self.navigationController.navigationBar.frame) + CGRectGetHeight([UIApplication sharedApplication].statusBarFrame);
self.tableView.contentInset = UIEdgeInsetsMake(shiftDist,0, 0, 0.0f);
self.tableView.contentOffset = CGPointMake(0, -shiftDist);

      



After that, there is a problem for rotation. You will notice that if you turn to landscape, the search bar will not be below the navigation bar. Here's how to fix it.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);{

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    CGFloat heightBeforeRotation = CGRectGetHeight(self.navigationController.navigationBar.frame) + CGRectGetHeight([UIApplication sharedApplication].statusBarFrame);
    CGFloat offsetY = self.tableView.contentOffset.y;

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
         CGFloat heightAfterRotation = CGRectGetHeight(self.navigationController.navigationBar.frame) + CGRectGetHeight([UIApplication sharedApplication].statusBarFrame);
         self.tableView.contentInset = UIEdgeInsetsMake(heightAfterRotation,0, 0, 0.0f);
         self.tableView.contentOffset = CGPointMake(0, heightBeforeRotation + offsetY - heightAfterRotation);
     }];
}

      

0


source


The problem is how the tab bar controller is presented. Tab bar controllers are special and should usually be the initial view controller in your storyboard. If the tab bar controller is not the initial view controller, it must be presented modally. In my test app, I changed the segue to "Present Modally". Then added a navigation controller for each Table View Controller with the button pressed, which rejected the tab bar modal controller.

enter image description here

- (IBAction)DoneButton:(id)sender {
    [self.parentViewController dismissViewControllerAnimated:YES completion:nil];
}

      



Apple Documentation "Browsing the iOS Controller Catalog"

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html

Adding a navigation controller to the tab bar interface

An application that uses a tab bar controller can also navigate controllers in one or more tabs. When you combine these two types of view controller in the same user interface, the tab bar controller always acts as a wrapper for navigation controllers .

The most common way to use a tab bar controller is to enable its view in the main application window. The following sections show you how to customize the main application window to include a tab bar controller and one or more navigation controllers. There are examples for this both programmatically and using the Interface Builder.

Transparency controllers are not supported in tab bar views, and tab bars never display content under their respective tab bar. Hence, if your navigation interface is embedded in a controller's tab bar tab, your content may hide the navigation bar if you adopt a full screen layout as described in the Full Screen Layout Appendix for Navigation Views, but it does not overlap the tab bar.

When embedding navigation controllers in a tab bar interface, you should only embed instances of the UINavigationController class and not system view controllers that subclass the UINavigationController class. While the system provides navigation controllers for selecting contacts, selecting images, and implementing other types of behavior, these view controllers are usually designed to be presented modally. For information on how to use a particular view controller, see the reference documentation for that class.

Tab Bar Controller Display Modified

It is possible (albeit unusual) to introduce a tab bar controller in your application . Tab bar interfaces are usually installed in the main application window and are updated only as needed. However, you could introduce a tab bar controller if your interface design looks like it justifies it. For example, to switch from your applications to the main mode of operation, to a completely different mode that uses the tab bar of the interface, you can represent the middle tab bar controller using a crossover.

When presenting the default tab bar controller, you always pass the tab bar as the first parameter to the presentModalViewController: animated: method. The tab bar controller must be configured before it is presented. Therefore, you must create root view controllers, customize them and add them in the same way as if you were installing the interface tab bar in the main window.

As with all other modally represented view controllers, the parent view dispatcher is responsible for firing its modally represented child in response to an appropriate user action. When dismissing a tab bar controller, remember that this removes not only the tab bar controller object, but also the view controllers associated with each tab. View controllers that are not visible are simply removed, but the view controller displayed in the currently visible tab also receives the usual viewWillDisappear: message.

For information on how to present view controllers (including navigation controllers), see "Representing View Controllers" Other View Controllers in the iOS Controller Programming Guide. For information on how to configure a tab bar controller for use in your application, see Tab bar controllers.

0


source







All Articles