I am creating a UINavigationBar which works fine. But, it scrolls with my UITableView, which I don't want. How can I anchor the navbar at the bottom of the screen, so that it does NOT scroll?
bottomNav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height, self.view.frame.size.width, 44.0)];
bottomNav.barStyle = UIBarStyleBlackOpaque;
[self.view addSubview:bottomNav];
It sounds like your adding it to the tableview instead of view that contains the tableview. If self is a tableview controller that is definitely your problem.
Yes, TechZen you were right. I needed to add it to my parent view controller:
bottomNav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - 20, self.view.frame.size.width, 44.0)];
bottomNav.barStyle = UIBarStyleBlackOpaque;
[self.parentViewController.view addSubview:bottomNav];
Related
In my split view application it is not possible to add search bar to the rootView of the split view
So i added search bar dynamically at the tableHeaderView of the ui table view as folows
searchBar = [[UISearchBar alloc] init];
searchBar.frame=CGRectMake(0, self.tableView.frame.origin.y, self.tableView.frame.size.width, 44);
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
When Scroll down: iThe tableHeaderView also scrolls down so search bar also scrolls
When Scroll top: The tableHeaderView also scrolls top so search bar also scrolls
I implemented code as follows to resolve this issue this helps only when scrolls down but when we scrolls the table view to topside it again move with the table view
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect rect = self.tableView.tableHeaderView.frame;
rect.origin.y = MIN(0, self.tableView.contentOffset.y);
self.tableView.tableHeaderView.frame = rect;
}
I need to stick the tableHeaderView/ Search bar at the top of the view always
How to do this
You can add tabBar separate with tableView
mySearchBar = [[UISearchBar alloc] init];
[mySearchBar setHidden:NO];
mySearchBar.placeholder = #"Search item here";
mySearchBar.tintColor = [UIColor darkGrayColor];
mySearchBar.frame = CGRectMake(0, 0, 320, 44);
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self.view addSubview:mySearchBar];
And tableView
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];
[self.view addSubview:tableView];
If You want to add in xib then
Put your searchBar in separate view and put that view above the table view. That means it stays constant.
I'm sure this has been answered before, but assuming you're using UITableViewController, you can make the view property be anything you want. So one approach would be to set up a container view with your search bar at the top and the table below it and make view be this container. By default, tableView returns view, so another detail you'd need to take care of is overriding the tableView property to return the actual table view (that you've stored in an ivar). The code might look something like this:
#synthesize tableView = _tableView;
- (void)loadView
{
[super loadView];
_tableView = [super tableView];
// Container for both the table view and search bar
UIView *container = [[UIView alloc] initWithFrame:self.tableView.frame];
// Search bar
UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
// Reposition the table view below the search bar
CGRect tableViewFrame = container.bounds;
tableViewFrame.size.height = tableViewFrame.size.height - searchBar.frame.size.height;
tableViewFrame.origin.y = searchBar.frame.size.height + 1;
self.tableView.frame = tableViewFrame;
// Reorganize the view heirarchy
[self.tableView.superview addSubview:container];
[container addSubview:self.tableView];
[container addSubview:searchBar];
self.view = container;
}
- (void)viewDidLoad{
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"ka1_046.png"]];
myImage.frame = CGRectMake(0, 0, 320, 460);
[baseView addSubview:myImage];
// create the UIToolbar at the bottom of the view controller
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 425, 320, 40)];
toolbar.barStyle = UIBarStyleBlackOpaque;
[baseView addSubview:toolbar];
How can i make Main VC's UIToolbar which has no navigation controller and no tabbarcontroller global for all VC's
There are several ways,
First: you could instead of presenting the new viewcontrollers, you could just add them to the self.view and minimize their size to fit the view controller minus the tabbar
Second: you could share the instance of the tab bar in some global class, such as app delegate, just create the tab bar in the app delegate and add a property around it, so that other VC can access it, and then at each viewDidLoad of a new VC you would add it
I am using a UISearchBar and once the user taps the bar (and the keyboard pops up) I want the everything besides the keyboard and UISearchBar to be greyed out. Similar to safari when the search is selected. I do not need past searches. See here:
Anyone have any ideas? I've looked through the questions here on StackOverflow and I can't find anything specific.
Thanks!
You can create a UIView that contains the UISearchBar. And then you can set UIView's backgroundColor to get the gray background.
And here's some sample code:
UIView *view1 = [[UIView alloc] init];
view1.frame = CGRectMake(0, 20, 320, 460);
view1.backgroundColor = [UIColor grayColor];
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, 320, 50);
[view1 addSubview:searchBar];
[searchBar release];
[self.window addSubview:view1];
[view1 release];
- (void)loadView
{
SettingsTitleBar=[[UINavigationController alloc] initWithRootViewController: self];
searchBar =[ [UISearchBar alloc] initWithFrame:CGRectMake(0, 44, 320, 40)];
searchBar.placeholder = #"Type your City Name";
searchBar.delegate = self;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
[searchBar setShowsCancelButton:YES];
[SettingsTitleBar.navigationBar addSubview:searchBar];
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 85, 320, 392)];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, 320, 302) style: UITableViewStyleGrouped];
[tableView setDelegate:self];
[tableView setDataSource:self];
[self.view addSubview:tableView];
}
I Have a UITableViewController,
I have utilized the first 44 pixels height for title bar, and then the next 40 pixels of height for search bar(44+40). These are added as navigation controller subviews. then i am adding my self.view at 85 pixel from top, finally tableview has been added as child to the self.view . But table view has been overlapped with the Searchbar. I dont what is wrong with it. I tried to change various yPositions of tableview and self.view but still nothing happened.
Can anyone able to help me out from this ?
Note : I dont want to add SearchBar into UITableviewHeader Section.
U can make the view of size 320X436 and add the tabble view and search bar in that view...
I wouldn't add subviews to the navigationBar. Add it to the view.
As a sidenote: anything you alloc (or retain or copy), you should release or autorelease
I had trouble with this too, initially. Don't worry about adding the searchBar as a subview. Simply do this:
self.tableView.tableHeaderView = searchBar;
The searchBar will be treated like a header. You'll want to change the CGRect's size to (0,0,320,44), however.
I am creating a view controller programmatically which is a UITabBarDelegate and contains a UITabBar.
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 200, 320, 49)];
[self.view addSubview:tabBar];
I don't really want to hardcode the frame values in case the screen resolution changes or I use the view in a different context with a different height, for example.
I'm vaguely aware I can create the tab bar without these values and probe the window and tabbar for height and width to calculate the desired values but I'm struggling to achieve this. What I want to achieve is a tab bar of the standard width and height correctly positioned at the bottom of the enclosing view.
Maybe something like this:
UITabBar *tabBar = [[UITabBar alloc] init]; //init with default width and height
tabBar.frame = CGRectMake(0,
self.view.frame.size.height - tabBar.frame.size.height,
self.view.frame.size.width,
tabBar.frame.size.height)]; //place it at the bottom of the view
[self.view addSubview:tabBar];
[tabBar release];
EDIT:
I've realised thet the size of a newly instantiated UITabBar is zero. Probably your best bet is to use a UITabBarController, as it already sizes your UITabBar accordingly:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[self.view addSubview: tabBarController.view];