UITableView: Painting-Behavior of Header- and Footer-View - ios

Why does the header and footer of a tableview always stays on top of the view hierarchy, but not the cells of the table?
Here is what I got:
A table view with custom cells, a footer and a header
A Navigationbar with a menu-Button on the right
When the user taps the menu-button, a semi-transparent menu (UIView) fades in from the top, but not over the navigationbar, only over the view of my tableviewcontroller
When the user tabs a menu-button, the menu slides back to (0,-menuHeight)
But when the menu is over the header, this menu-region is behind my header
I anchored the menu on the view of my tableviewcontroller, beacause I want the navigationbar allways be visible. the solution to anchor it on the navigationbar solves the problem with the header-view, but covers the navigationbar.
Has anyone an idea how to solve this problem? Why are the cells painted correctly?

You should probably not interfere directly with the internal view hierarchy of UITableView. Apple is free to order a table view's subviews as they see fit (and change it in future releases).
Instead, place the table view and your menu view into a common container view and make the latter the main view of your view controller. That way, you can be certain that your menu view will always be above the table view.

I changed my design, but now I do not get touch-events in my table.
But when I add this method - I see, that I'm touching the tableviewcontroller...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"ShieldingViewController received touch");
[self.buttonMenu shieldingViewTouched];
}
Here is what I changed:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0;
UIView *rootView = [[UIView alloc] initWithFrame:viewFrame];
rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Setup the table view
UITableView *newTableView = [[UITableView alloc] initWithFrame:viewFrame style:self.tableView.style];
newTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
newTableView.backgroundColor = [UIColor darkGrayColor];
UIView *menuLayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
self.tableView = newTableView;
[rootView addSubview:self.tableView];
[rootView addSubview: menuLayerView];
self.view = rootView;
[[CustomNavigationController instance] setCurrentViewForMenu: menuLayerView];
[[CustomNavigationController instance] showMenuInNavigationBarForController:self];
[newTableView release];
[menuLayerView release];
[rootView release];
}

Related

iOS hidesBarsOnSwipe status bar background color

When I swipe and hide the navigation bar with the hidesBarsOnSwipe property the status bar has a clear background. How can I set the background of the status bar to the same color as the navigation bar? Here are a few pictures showing my problem, this is all contained in a UITableViewController.
Separate
Separate picture, looks like one big one.
I've come across the same issue, and was able to solve it. I'm fairly new to iOS dev, and I don't imagine this solution to be foolproof. I couldn't find any good answers elsewhere, so here's how I overcame it:
I converted from a UITableViewController over to UIViewController with a nested UITableView. Note, double check that the delegate to the child tableview is set to the UIViewController.
I Added a view with a height of 20px and a background colour that you want to set as the "background" to the status bar. Set the constraints on that view as follows:
On your table view, set the constrains to be basically full screen. One important note here, the top constraint is to "Top Layout Guide.Top" and not to "Top Layout Guide.Bottom". By default I believe this constraint ties to the bottom. Double clicking on the constraint allows you to adjust it to the top. Without this, any table header cells weren't positioned properly for me
Hope that helps.
Adding to George Huber's answer. I solved this issue programmatically by adding a 20pt height UIView as a subview of the navigationController's view property -- in viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *statusBarBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 20)];
statusBarBG.backgroundColor = [UIColor navBar];
[self.navigationController.view addSubview:statusBarBG];
// REST OF CODE
}
Per skg's answer, I add a relative height for status bar according to iOS version.
self.navigationController.hidesBarsOnSwipe = true;
// add a UIView as subView to navigationController
CGFloat statusBarHeight;
if (#available(iOS 13, *)) {
NSArray *windows = UIApplication.sharedApplication.windows;
UIWindow *keyWindow = nil;
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
keyWindow = window;
break;
}
}
statusBarHeight = keyWindow.windowScene.statusBarManager.statusBarFrame.size.height;
NSLog(#"statusBarHeight: %f", statusBarHeight);
} else {
statusBarHeight = UIApplication.sharedApplication.statusBarFrame.size.height;
}
UIView *statusBarBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), statusBarHeight)];
statusBarBG.backgroundColor = [UIColor systemBackgroundColor];
[self.navigationController.view addSubview:statusBarBG];

Adding childContainerView disables UITableView

I have a chat that's setup as a UIViewController with a tableView added to it's view. This was working fine. I've since subclassed this viewController and added a childViewController as a header of the table:
- (void) createClassVideoView {
CGRect rect = CGRectMake(0.0f,
0.0f,
self.view.frame.size.width,
kWSClassVideoHeight);
WSClassVideoController *classVideoController = [[WSClassVideoController alloc] initWithFrame:rect mainUser:[[WSConversationManager shared] teacherObject] secondaryUsers:[[WSConversationManager shared] students]];
[self addChildViewController:classVideoController];
[self.view addSubview:classVideoController.view];
[classVideoController didMoveToParentViewController:self];
self.classVideoController = classVideoController;
}
It's not set as the actual tableHeaderView. It's just added as a childViewController to the subclass of the chat view controller, and I've just reset the frame of the tableView to accomodate it. This "header view" works fine and receives touches.
However, it's since disabled the superclass tableView's touches. The tableView displays, but I can't scroll it and tapping the UITextField does nothing.

Banner hugged to the bottom

I'm using AdMob to display ads in my app. The problem is i cant seem to hug to the bottom always.
How can i make the bannerView_ stay at the bottom when people are scrolling?
This is my banner frame:
[bannerView_ setFrame:CGRectMake(0.0, [[UIScreen mainScreen] bounds].size.height-bannerView_.frame.size.height, 320, 50)];
My basic suggestion is.
First create scrollView and add all your controllers on it and add scrollView in self.view as subView.
Then after ( over scrollView) create UIView (name is bannerView) and also add this view in self.view as subView.
So basically this bannerView is overlapping on the scrollView and it stay visible when you scroll up/down.
You can add this bannerView_ to UIWindow. i.e to [[UIApplication sharedApplication].windows objectAtIndex:0]; .
By doing so bannerView_ will be added and will be independent of other views.
This will be helpful in case you want to present your ad on different view controllers and don't want to handle its interference with the base view of different view controllers.
You may follow my code in which banner will be stick at bottom
GADBannerView *bannerView_ = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait] autorelease];
bannerView_.adUnitID = AdMob_ID;
bannerView_.rootViewController = self;
UIView *bannerView=[[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height - bannerView_.frame.size.height, self.view.frame.size.width, bannerView_.frame.size.height)];
bannerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[bannerView addSubview:bannerView_];
[self.view addSubview:bannerView];
[self.view bringSubviewToFront:bannerView];
[bannerView release];
[bannerView_ loadRequest:[GADRequest request]];

How to also animate subview in navigation bar?

I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.

Replacing self.view with new UIView shows black view

I want to change the existing view in a UIViewController to a new view. The new view contains the old view and a little banner view.
Doing this fairly simple change leaves me with a black view.
My code looks like this
UIView *existingView = self.view;
UIView *newView = [[UIView alloc] initWithFrame:existingView.frame];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 50), 320, 50)];
CGRect existingViewFrame = existingView.frame;
existingViewFrame.size.height -= 50;
existingView.frame = existingViewFrame;
[newView addSubview:existingView];
[newView addSubview:bannerView];
self.view = newView;
However when switch Tabs and come back to the view which changed the view is shown just like I want. I guess I need to set a flag or something to tell the controller to redraw it's (new) view.
Edit
I wrote an simple example for this problem. You can find it on GitHub: https://github.com/Oemera/ChangeView
You did not say where you do this. It may be that you need to save the original view's super view, then add the new view to that views subViews array. I'm betting that is the problem.

Resources