I understand that there is a tableHeaderView property, but when ever I add my view to that, it is not hidden above the scroll area.
What I would like to have is, my custom view shown when you pull down the tableview and hold and you see my UIView brought into view. This is done on many apps to put a logo or such slightly hidden until a user pulls down on the tableview (Twitter/Facebook when you pulldown).
I am currently using the following and it is not putting it out of the view:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
l.text = #"Hidden Text";
l.textColor = [UIColor redColor];
self.tableView.tableHeaderView = l;
[l release];
Since UITableView is actually a UIScrollView with some extra functionality, you can use contentInset to obtain the effect you want. The trick is to use a negative value for the top inset. This will normally hide your header view, but it will still be viewable when the table bounces.
So, after you add the label to the header view, just set the contentInset like this:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 20)];
l.text = #"Hidden Text";
l.textColor = [UIColor redColor];
self.tableView.tableHeaderView = l;
//add this
[self.tableView setContentInset:UIEdgeInsetsMake(-l.bounds.size.height, 0.0f, 0.0f, 0.0f)];
[l release];
The best solution here is to add your view to the header, as you had mentioned you tried, and then in your controller's viewDidLoad actually scroll the tableview downward programmatically until the header view you wanted hidden is hidden. This can be done a number of different ways. The easiest is probably:
[self.tableView setContentOffset: CGPointMake(0, myHeaderHeight)];
Simply have a 0-height header view, and then have a subview of that be positioned with a negative y, and so that the bottom edge of the subview is the top of the view.
UIWindow* window = [[UIApplication sharedApplication].delegate.window;
[window addSubview: your-overlayview];
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;
}
I'm having a lot of trouble dynamically adding subviews to a UIScrollView. The scroll view works fine with content created in a NIB but since the subviews to be displayed depend on data in my application (a mixture of images, labels, radio buttons, etc.) I need to be able to create and display them dynamically.
According to everything I've read it seems pretty straightforward on various sites and in the Apple documentation. In the view controller's viewDidLoad, I've added the following code,
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setFrame:CGRectMake(50, 50, 100, 40)];
[testLabel setText:#"My Test label"];
[scrollView addSubview:testLabel];
[testLabel release];
The label will not appear in the scroll view at all but if I add the testLabel to self.view, then it appears (but not in the scrolling content obviously). I even tried adding the code to viewDidAppear in case I misunderstood the order of events with no luck.
When I checked the debugger, I noticed that the address of the scroll view is 0x0 which I assume means its null for some reason which would explain why its not working. I was under the assumption that if I connected this scrollView pointer to the actual scroll view in IB, it would be automatically assigned the correct address. Is this incorrect? If this is the case, how do I go about getting the address of the view?
-- UPDATE --
Thanks for all the feedback. I checked everything as everybody suggested and it was certainly all correct. I didn't need to set the size of the content as I had some other dummy labels (for testing that the scrolling was working) in the NIB. But I'll remember that for later on :-)
Interestingly, though, after checking the code again and not making any changes, I ran it again and it just worked!! Not sure why but I'll post the reason if I ever figure it out...
When you use scrollView you need to set the content size by doing:
scrollView.contentSize = CGSizeMake(#width,#height);
In this case the size should be bigger the 50,50 if you wanna see the label
Hope it helped
As described in your question You are working with a scrollview which you have added in the XIB.
When you declare an outlet as IBOultlet UIScrollView* scrlvDynamicContent;
and connect the same to your scrollview in interface builder,ideally you will get the allocated scrollview after viewdidLoad method is called.
so doing something as
-(void)viewDidLoad{
[super viewDidLoad];
[self createDynamicView];
}
where you can generate the dynamic view as follows
-(void)createDynamicView{
CGFloat yOffset = 0;
for (int i=0;i<5;i++) {
yOffset += 5;
UILabel* lblHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(8, yOffset, 310, 21)];
[lblHeaderTitle setTextAlignment:UITextAlignmentLeft];
[lblHeaderTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0f]];
[lblHeaderTitle setText:[currentDict valueForKey:#"TITLE"]];
[lblHeaderTitle setTextColor:[UIColor blackColor]];
[scrlvDynamicContent addSubview:lblHeaderTitle];
[lblHeaderTitle release];
//INCREMNET in yOffset
yOffset += 25;
[scrlvDynamicContent setContentSize:CGSizeMake(320, yOffset)];
}
Just make sure that scrlvDynamicContent is connected properly to its outlet set to its file owner
[scrollView setFrame:CGRectMake(0,0,320,460)];
[scrollView setContentSize:CGSizeMake(100, 40)];
[self.view addSubview:scrollView];
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setFrame:CGRectMake(50, 50, 100, 40)];
[testLabel setText:#"My Test label"];
[scrollView addSubview:testLabel];
[testLabel release];
I have a 320x460 view with a number of buttons, depending on the button pressed, a 280x280 view pops up over the 320x460 view (similar to the behavior of the UIAlertView) using code like this:
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(20, 200, 280, 280)];
overlayView.backgroundColor = [UIColor whiteColor];
[overlayView autorelease];
[overlayView addSubview:label]; // label declared elsewhere
[overlayView addSubview:backgroundImage]; // backgroundImage declared elsewhere
//... Add a bunch of other controls
[label release];
[backgroundImage release];
//... Release a bunch of other controls
[self.view addSubview:overlayView];
Everything works fine displaying the overlayView and all its controls.
The question I have is, how do I get rid of the overlayView once it's displayed? I want to make it not only not visible but to remove it completely, since the user will be popping up the overlayView repeatedly during use.
You need access to overlayView to remove it, I'd suggest adding this to the create side:
overlayView.tag = 5; // Or some other non-zero number
Then later you can use it like this:
-(void)removeOverlayView
{
UIView *overlayView = [self.view viewWithTag:5];
[overlayView removeFromSuperview];
}
- (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.
How can I change the line's color that separates the navigation bar and the view?
For instance flickr changed it to gray (http://www.geardiary.com/wp-content/uploads/2009/09/Screen-shot-2009-09-08-at-8.00.06-AM.png)
By default mine is always black...
Thanks in advance for your help,
nico
They used a custom bottom bar and not the Apple provided ones. I dont know your setup, but if you can make or draw your own custom view however you want (you can do this), and stick buttons on it (you can do this too), then you have a toolbar
#define TOOLBAR_HEIGHT 44
CGRect frame = CGRectMake(self.view.bounds.size.height - TOOLBAR_HEIGHT, 0.0, self.view.bounds.size.width, TOOLBAR_HEIGHT);
UIView *customBottomBar = [[UIView alloc] initWithFrame:frame];
[customBottomBar setBackgroundColor: [UIColor grayColor]];
UIButton *button = [[UIButton alloc] initWithFrame:<frame goes here>]
... <button setup>
[customBottomBar addSubview:button];
[button release];
...<more buttons>
...<more buttons>
[self.view addSubview:customBottomBar];
[customBottomBar release];
And to answer your question, you can add whatever you want to any view, so while the way I just suggest is the most customizable, you might just want to place a 1pixel high colored bar at the right spot (on top of the existing toolbar) by doing this:
CGRect frame = CGRectMake(self.view.bounds.size.height - TOOLBAR_HEIGHT, 0.0, self.view.bounds.size.width, 1);
UIView *customLine = [[UIView alloc] initWithFrame:frame];
[customLine setBackgroundColor: [UIColor grayColor]];
[self.view addSubview:customLine];
[customLine release];