I am trying to implement UISearch bar but I have some problems.
When app starts I add table view like this:
ChannelTableViewRootController *firstViewController = [[ChannelTableViewRootController alloc] initWithStyle:UITableViewStylePlain];
navigationController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
navigationController.navigationBar.hidden = NO;
UIView *uv = self.view;
[uv addSubview:[navigationController view]];
I add UISearch bar in xib. Connect it to UISearchBar variable from my header file. Set delegate to self, and try to add it to header of tableview:
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
And when I make object manually 'searchBar = [[UISearchBar alloc]init]; // + code from above' I got displayed search bar like on image but nothing happends and can't put it over that header. I am trying to make search like in address book with scopes etc.
Have you tried to create the searchbar with the initWithFrame. Try giving Values to it and adding subView to the view not on header. I think you have some mistake in positioning.
You also specified that you added UISearchbar in xib and also you have allocated in the coding also. Dont do that its a wrong way. Do any one of them i suggest you if you add tableView programatically then add searchbar programatically. Or assign them both in Xib.
Otherwise have a look at this tutorial
http://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in.html
Related
I'm looking for opinions on the best way to implement the following functionality. I have a UIViewController with a UITableView in Grouped Format. Each TableViewCell contains an NSString. When the User taps on a cell I'd like to in my didSelectRowForIndexPath method popup a UIView with a single textfield, that's prepopulated with the NSString in the given cell that was selected. The reason for displaying the UIVIew is I want it to be about 280 x 380 frame and still see some of the UITableView.
The goal being that it behaves like a ModalViewController except for the iPhone. Does anyone have any suggestions on how to implement this behavior or if there is a better implementation?
Create the UIView (with the UITextField inside) beforehand, and make it hidden:
// Of course, these instance variable names are made up, and should be changed
self.myModalView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 380)] autorelease];
[self.view addSubview:self.myModalView]
self.myTextField = [[[UITextField alloc] init] autorelease];
[self.myModalView addSubview:self.myTextField];
self.myModalView.hidden = YES;
Then, when the user selects a row, populate the text field and show the modal view:
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
// Replace stringAtIndexPath with however your data source accesses the string
NSString* myString = [self stringAtIndexPath:indexPath];
self.myTextField.text = myString;
self.myModalView.hidden = NO;
}
If you want to get fancy, you can do some CATransition stuff before showing the modal view.
I think you can use "addSubView" in UITableView. Add the ModalView to your UITableView directly. It will work.
Use some animation to implement this effect. When the UIView appears, it will lift the UITableView, like some keyboard behavior. So, you have to addSubView on the self.view and modify the UITableView's frame. And, the UITableView should be a child view of self.view, if self.view is the same as the UITableView, then you has no self.view for adding this UIView.
I have a UITabBarController which has been created programatically, which has 6 tabs. As such the MoreNavigationController is automatically created to take care of having more than 5 tabs. Everything looks fine when the MoreNavigationController is displayed, but when I select one of these rows to push the view controller on to the stack, the cell image (tab bar image) disappears. When I pop that view controller, the image remains hidden until the pop animation is completed, at which point the image suddenly appears again.
This is fairly old code and I wouldn't do it this way these days, but everything works except for this last little thing so I'm pretty hesitant to rip out all the code and do it another way. Can anyone suggest what I might be doing wrong?
An example of creating one of the tab bar view controllers:
InfoViewController* infoViewController = [[InfoViewController alloc] init];
infoViewController.tabBarItem.image = [UIImage imageNamed:#"90-life-buoy.png"];
infoViewController.tabBarItem.title = #"More Info";
infoViewController.title = #"More Info";
UINavigationController* infoNavController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[infoViewController release];
Creating the tab bar:
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:outdoorsNavController, peopleNavController, citiesNavController, landscapesNavController, infoNavController, basicsNavController, nil];
[window addSubview:tabBarController.view];
EDIT: Doesn't seem to make any difference whether I use retina (#2x) images or not.
The issue is because you're wrapping your InfoViewController in a UINavigationController.
When you click on the table row in MoreNavigationController, the controller uses the tabBarItem in UINavigationController while it does its transition. Because this is nil (in your code), the image in MoreNavigationController disappears. When the transition finally finishes, MoreNavigationController picks up the tabBarItem in InfoViewController
Try this:
InfoViewController* infoViewController = [[InfoViewController alloc] init];
infoViewController.tabBarItem.image = [UIImage imageNamed:#"90-life-buoy.png"];
infoViewController.tabBarItem.title = #"More Info";
infoViewController.title = #"More Info";
UINavigationController* infoNavController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
//Set the tabBarItem for UINavigationController
infoNavController.tabBarItem = infoViewController.tabBarItem
[infoViewController release];
Here's a video reproducing and fixing the issue:
Item 7 has an empty tabBarItem.image while Item 6 has tabBarItem.image set
I'm not sure, but have you tried setting the NavigationCotroller's .tabBarItem?
Not sure if I understand correctly, but on the top of my head here are some suggestions (The More Tab Bar item shouldn't disappear at all times, that is created automatically)
Have you tried subclassing your UINavigationController that is being used for the MoreController's place and set its tab bar items properties there, therefore making sure you have control over its lifetime ?
Subclass your UIViewController's that you wish to push onto the navigation stack and have them use the same tab bar item ?
Mimic the default functionality, create your own UITableViewController to act as the More Controller, and at each tap of the rows, do what you want, also in a custom way.
PS: Try setting images name without the .png extension. This way you will automatically load the #2x resource as well. Eg: [UIImage imageNamed:#"90-life-buoy"]
Link
I am using a UITabBar control from library in one of my view (note that I am not using UITabBarController but the UITabBar control).
Now, I am adding two tabBar items to this tabBar.
I have created controller class for this view (.m and .h) files and used delegates in the .h file.
In the .m file I have used the following function:
(void)tabBar:(UITabBar *)TabBarControl didSelectItem:(UITabBarItem *)FirstView
I have assigned tag = 0 and tag = 1 to respective tabBar items.
What I want to do is that, on click of first tabBar item I want to show a view and click of another tabBar item, I want to show another view.
So, in the above function I am checking that if the tag of clicked tabBar item is 0 than I will show one view else I will show another view.
I am showing the view as following:
Team1Scoreboard *tempTeam1Scoreboard = [Team1Scoreboard alloc];
tempTeam1Scoreboard = [tempTeam1Scoreboard initWithNibName:#"UserTeamScoreboard" bundle:[NSBundle mainBundle]];
self.cntrlTeam1Scoreboard = tempTeam1Scoreboard;
[tempTeam1Scoreboard release];
UIView *theWindow = [self.view superview];
[self.view removeFromSuperview];
[theWindow addSubview:self.cntrlTeam1Scoreboard.view];
Now the problem is that, when I click on any of the tabBar item, it will load the correct view but the tabBar itself will be disappeared as I am adding the view to window itself.
Please help me so that I can load correct view and also my tabBar itself is visible.
The TabBar is disappearing because it's a child of the view which you are then adding a new child to and the new child is sized the same as the parent. Did that make sense? Ok, look at it this way:
You have ViewA and ViewA has a couple of labels and a TabBar. ViewA is managed by ViewControllerA. In ViewControllerA you are creating an instance of ViewB and calling ViewControllerA.view addSubView:instanceOfViewB, right? Before doing that, you will want to resize ViewB.
Try something like this:
ViewControllerB *viewControllerB = [[ViewControllerB alloc]initWithNibName:#"ViewB" bundle:nil];
CGRect frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 40);
viewControllerB.view.frame = frame;
[self.view addSubview:viewB.viewControllerB];
Basically it should be close to what you are doing, but I'm setting the size to be 40 px less (whatever you need to remove the tab bar).
Hey all. I'm still pretty new to iPhone development, and I'm having a bit of trouble figuring out how to change the title of my Navigation Bar. On another question on this site somebody recommended using :
viewController.title = #"title text";
but that isn't working for me...Do I need to add a UINavigationController to accomplish this? Or maybe just an outlet from my UIViewController subclass? If it helps, I defined the navigation bar in IB and I'm trying to set its title in my UIViewController subclass. This is another one of those simple things that gives me a headache. Putting self.title = #"title text"; in viewDidLoad and initWithNibName didn't work either. Anybody know what's happening and how to get it happening right?
Thanks!
The view controller must be a child of some UINavigationController for the .title property to take effect. If the UINavigationBar is simply a view, you need to push a navigation item containing the title, or modify the last navigation item:
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:#"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
or
bar.topItem.title = #"title text";
if you are doing it all by code in the viewDidLoad method of the UIViewController you should only add self.title = #"title text";
something like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"title";
}
you could also try self.navigationItem.title = #"title";
also check if your navigationItem is not null and if you have set a custom background to the navigationbar check if the title is set without it.
There's one issue with using self.title = #"title";
If you're using Navigation Bar along with Tab bar, the above line also changes the label for the Tab Bar Item. To avoid this, use what #testing suggested
self.navigationItem.title = #"MyTitle";
If you want to change navbar title (not navbar back button title!) this code will be work.
self.navigationController.topViewController.title = #"info";
If you want to change the title of a navBar inside a tabBar controller, do this:
-(void)viewDidAppear:(BOOL)animated {
self.navigationController.navigationBar.topItem.title = #"myTitle";
}
In my navigation based app I do this:
myViewController.navigationItem.title = #"MyTitle";
I had a navigation controllers integrated in a TabbarController. This worked
self.navigationItem.title=#"title";
By default the navigation controller displays the title of the 'topitem'
so in your viewdidload method of your appdelegate you can. I tested it and it works
navController.navigationBar.topItem.title = #"Test";
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:#"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
This code worked.
If you are working with Storyboards, you can click on the controller, switch to the properties tab, and set the title text there.
I guess you need a dynamic title that is why you don't set it in IB.
And I presume your viewController object is the one specified in the NIB?
Perhaps trying setting it to a dummy value in IB and then debug the methods to see which controller has the dummy value - assuming it appears as the title...
From within your TableViewController.m
:
self.navigationController.navigationBar.topItem.title = #"Blah blah Some Amazing title";
For all your Swift-ers out there, this worked perfectly for me. It's notably one of the shorter ways to accomplish setting the title, as well:
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentLineItem" {
print("Setting Title")
var vc = segue.destinationViewController as! LineItemsTableViewController
vc.navigationItem.title = "Line Item"
}
}
I have a table view setup which currently, when being flickered up, has its sections flush up against right underneath the status bar, instead of flushing against the the navigation bar. I'm not sure if this is the proper behavior, but most applications have the Section Title flush properly below the navigation bar when it's slid into view.
What's the right way to correct this instead of downsizing the tableView arbitrarily?
* EDIT *
Related to a thread I created in Broken cell with an odd strikethrough?. This problem plus the 'cell strike-through' problem occurs when I set my Navigation Bar to a Translucent Black. When it's Black Opaque or Normal, such a problem does not exist. I'm not sure if that's a result of something else in my code or an issue with the SDK.
Sounds like you either don't have the bounds set properly in IB, or your springs-and-struts aren't correct. Is this the top level of the UIViewController, or a subview? Are you using a UINavigationController? If you test the interface in IB, does it look okay?
I had a similar problem and it gets fixed automatically by setting this
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
As I indicated in the other post, I suspect the hierarchy of views under the UINavigationController has become disrupted.
The layoutSubviews in the content view (which contains the navigation bar and your UITableView) of the UINavigationController should be sizing the UITableView such that it doesn't overlap the navigation bar. In your case, I'm guessing that either the UITableView is resizing itself afterwards or the layoutSubviews doesn't know about the UITableView for some reason and the UITableView is passing underneath the navigation bar, causing the alignment issue you are seeing.
I had the same problem on iOS 5.1 using the following code:
Create the navigation controller & add a table view
UINavigationController *navigationController = [[UINavigationController alloc] init];
[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
[navigationController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[navigationController.navigationBar setTranslucent:TRUE];
[navigationController setNavigationBarHidden:NO animated:NO];
[self presentModalViewController:navigationController animated:YES];
MyTableViewController *aTableViewController = [[[MyTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
aTableViewController.navigationItem.rightBarButtonItem = buttonItem;
[navigationController pushViewController:aboutTableViewController animated:YES];
Add a Table Header View to the table
ATableHeaderView aTableHeaderView = [[[ATableHeaderView alloc] initWithFrame:aboutTableView.frame] autorelease];
[aTableHeaderView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin];
[aTableHeaderView sizeToFit];
[aTableView setTableHeaderView:aTableHeaderView];
Inside the table header view I added some labels
UILabel *aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(x,y, width, height)] autorelease];
[aLabel setText:aString];
[aLabel setAutoresizesSubviews:YES];
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
I ended up with the table header content under the navigation bar. Changing the navigation bar to solid black fixed it. But this is not what I wanted. After some trial and error I removed the line:
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
From the setup of the UILabel in the header and the problem is fixed. I have a translucent header and the content of the table is positioned properly.