I don't want to use the toolbar of the UINavigationController
. I want to use a separate UIToolbar
instead.
Requirements:
- Always visible on screen
- Should stay on it's position at the bottom of the
UIView
(like the toolbar of theUINavigationController
) - should adapts it's width (e.g. after rotation)
- no IB/Storyboard solution
- Additionally: Don't hide the content of the
UITableView
I want to use Auto Layout for this. Despite my code is in C#, you can always provide your solution for Objective-C.
This works in viewDidLoad
on a UIViewController
, but not in viewDidLoad
on a UITableViewController
:
UIView toolbar = new UIView ();
toolbar.BackgroundColor = UIColor.Red;
toolbar.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddSubview (toolbar);
NSLayoutConstraint toolbarBottom = NSLayoutConstraint.Create (toolbar, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, BottomLayoutGuide, NSLayoutAttribute.Top, 1, 0);
NSLayoutConstraint toolbarLeft = NSLayoutConstraint.Create (toolbar, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 0);
NSLayoutConstraint toolbarRight = NSLayoutConstraint.Create (toolbar, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 0);
NSLayoutConstraint toolbarHeight = NSLayoutConstraint.Create (toolbar, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 40);
this.View.AddConstraints (new NSLayoutConstraint[] { toolbarBottom, toolbarLeft, toolbarRight, toolbarHeight });
For test reasons I used an UIView
instead of a UIToolbar
. The results are very similar. On an UIViewController
the red view is shown. On an UITableViewController
it doesn't appear at all.
I made another test without using Auto Layout:
RectangleF toolbarFrame = new RectangleF (0, this.View.Bounds.Height - 44, this.View.Bounds.Width, 44);
UIView toolbar = new UIView (toolbarFrame);
toolbar.BackgroundColor = UIColor.Red;
View.AddSubview (toolbar);
Here the UIView
is shown but it is on a fixed position in the table view and the separator line shines through. Not that what I want, but it seems possible to place an UIToolbar
on an UITableView
...