How to programatically offset UItableviewcontroller with navbar - ios

How can I programatically add a navbar to a UITableViewController and offset the actual table view to where it starts right where the navbar ends. This is what i've done so far:
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
[self.view addSubview:myBar];
[self.tableView setContentInset:UIEdgeInsetsMake(60, 0, 0, 0)];
}
but this is the result:
The reason I cannot just simply imbed a navigation controller is because im using this UITableViewController as a UIView for a childviewcontroller. Im doing this to get a paged UIScrollView much like snapchat. Whenever I try to just embed the navigation controller into this UITableViewController it doesnt show up during simulation.
For any further details on what Im trying to accomplish You can check out https://github.com/wannabegeek/PageViewController since that is the project im getting this off of.
Thanks!

Related

How to Set SubView Size Same as Navigation bar Size?

Here I have created Navigation bar with height 50 and width as view size.Now, I want to add a subview in the navigation bar with same navigation bar size.How to set the frame size for subview?
ViewController.m
-(void)ViewDidLoad
{
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[self.view addSubview:navbar];
}
Firstly, your code won't respond to the orientation changes (portrait / landscape handling) nor support split screen. To fix it, instead of setting frame in viewDidLoad (where btw you do not call super!) write:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.navBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
}
Then as it is a different view, you should create a subclass of your view. The code will work in a way as #raghu wrote, but it won't be easily maintanable and will be of low quality. Separate of concerns!
Inside your UIView subclass you should implement init methods and layoutSubviews where you should set a proper frame.

Table View Hide Searchbar until scrolled

I got a table view set up, and a working search bar - NO XIB OR STORYBOARD involved. Above the search bar I have label showing the numbers of entries in the table view and some other stuff. Well now I want the search bar + label hidden until the user scrolls up (like in Music App). This is the setup of my search bar
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.showsCancelButton = YES;
[self.view addSubview:self.searchBar];
self.searchBar.delegate = self;
self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height,0, 0, 0);
[self.tableView setTableHeaderView:self.searchBar];
[self.tableView setContentOffset:CGPointMake(0,88) animated:YES];
[self.zsearchDisplayController setActive:NO animated:YES];
This is my label:
tableCountDisplay = [[UILabel alloc]initWithFrame:CGRectMake(5, -44, 155, 44)];
The label is already hidden until the user scrolls. The problem is - I can't get the search bar to hide. If I do
[self.tableView setContentOffset:CGPointMake(0,88) animated:YES];
Then search bar and label are hidden but also the first element of my table view...
If I do 44 or 0 (doesn't matter which of them)
[self.tableView setContentOffset:CGPointMake(0,44 or 0) animated:YES];
the label is hidden, and everything else is visible. Technically 0,44 should be the right offset, but it does not work for some reason.
I'd be really happy about some help!
One of the solutions is to add your searchBar and your label as subViews to a UIView. Then set this UIView as the TableHeaderView. My working sample looks like this:
Screen before scrolling:
Screen after scrolling:
There is not a way to maintain the header of a tableView fixed
1- could use an UIViewController instead of UITableViewController.
2- add subview (UIView) for header (add the searchBarView in this view).
3- and add another subview for the tableview.
What about
tableView.contentOffset = CGPoint(x: 0, y: (tableView.tableHeaderView?.frame.size.height ?? 0))
on wiewWillAppear: ;)

ios - UIView to cover full screen, to cover the tabs from a UITabBarController

I have a UITabBarController and I want to add a UIView as a subview but I want that view to cover the whole screen including the tabs on the bottom. All attempts I have done result in the view cover everything except the tabs on the bottom.
Not sure what you have tried but, if you are trying to add the view from a UIViewController that is inside the UITabBarController then you should use:
UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
self.tabBarController.view.frame.size.width,
self.tabBarController.view.frame.size.height)];
[self.tabBarController.view addSubview:coverView];
//and maybe [self.tabBarController.view bringSubviewToFront:coverView];

UIRefreshControl hidden / obscured by my UINavigationController's UINavigationBar

I'm attempting to use a UIRefreshControl inside my UITableViewController which itself is inside a UINavigationController, which has its hidesNavigationBar property set to NO (so the navigation bar is visible).
The UIRefreshControl works, but is obscured by the UINavigationBar. I'm surprised I can't find anyone else who has run into this problem.
Possible relevant points:
I set the rootViewController of my UIWindow to be my UINavigationController.
I set the initial view controller of the UINavigationController by setting the viewControllers property of the UINavigationController.
My UITableViewController subclass is instantiated with a nib.
I instantiate my UIRefreshControl in the viewDidLoad method of my UITableViewController subclass. I set the refreshControl property of the UITableViewController subclass in this method.
The UIRefreshControl works perfectly fine, and I can see a portion of it, but it is obscured by my UINavigationBar. It looks completely normal if I set hidesNavigationBar to YES (but I don't want to hide it).
Edit:
The code used to create and position my UIRefreshControl is:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self
action:#selector(toggleRefresh:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
This code snippet is in the viewDidLoad method of my UITableViewController subclass, which is a child view controller of a UINavigationViewController.
For those targeting iOS 7, there seems to be a new issue present where the UIRefreshControl is drawn behind the UITableView's backgroundView. I experienced this both when initializing the UIRefreshControl programatically and from a storyboard. A simple workaround is to update the zPosition of the UIRefreshControl in viewDidLoad of your UITableViewController:
self.refreshControl.layer.zPosition = self.tableView.backgroundView.layer.zPosition + 1;
I've find a real solution, here it is:
I've a UIViewController inside a UINavigationController with a translucent NavigationBar. Inside the UIViewController there is the UITableView.
I want to add a UIRefreshControl but when I do it, it's hidden by the NavigationBar, like you explain.
Here is my code to make it work:
// Add a UITableViewController
self.tblViewController = [[UITableViewController alloc] init];
// Set the UITableView to it
self.tblViewController.tableView = self.tblView;
// Initialize the UIRefreshControl and set it's method
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(refreshTable) forControlEvents:UIControlEventValueChanged];
// Set the RefreshControl to the UITableViewController
self.tblViewController.refreshControl = self.refreshControl;
// Here is the thing ! Just change the contentInset to push down the UITableView content to 64 pixels (StatusBar + NavigationBar)
self.tblView.contentInset = UIEdgeInsetsMake(64.f, 0.f, 0.f, 0.f);
With this code, your UITableViewController will show the RefreshControl perfectly and keep the translucent NavigationBar effect when you scroll down the cells.
It looks like a bug to me, because it only occures when the contentOffset property of the tableView is 0
see this question
UIRefreshControl not showing spiny when calling beginRefreshing and contentOffset is 0
I fixed that with the following code (method for the UITableViewController) :
- (void)beginRefreshingTableView {
[self.refreshControl beginRefreshing];
if (self.tableView.contentOffset.y == 0) {
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){
self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
} completion:^(BOOL finished){
}];
}
}
In iOS 7, self.view is under the navigationBar, except that you write something as follows,
self.edgesForExtendedLayout = UIRectEdgeNone; // or UIRectEdgeAll & ~UIRectEdgeTop
or
self.navigationViewController.navigationbar.translucent = NO;
Using #Jonathan's answer I got this working well. But since I am using storyboard I set the content inset there like so: Which in case someones needs:
(xcode 6.4)
Do not call -setTranslucent: on your UINavigationBar. Then, your refresh control will be positioned properly, below the navigation bar.

UIView appearing before navigation transition is over

I'm writing an iPhone app with a table inside a navigation controller. When the user clicks one of the cells in the main screen a UIView on top of the incoming view controller is created (it's like a toolbar).
self.toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
toolbar.backgroundColor = [UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1.0];
[self.navigationController.view addSubview:toolbar];
The problem is that the view appears before the transition to the new view controller is complete and the effect is pretty weird. I suppose this is due to the fact I add the view to the navigationController,but I need to do this otherwise the bar would scroll together with the table and instead I want it to be fixed.
Any suggestion?
I've found a possible solution: add the toolbar as TableHeaderView and follow iOS: Add UIView to UITableView
Any other better solution is more than welcome

Resources