I have a UITableView whose data have sections. I display an overlay view on top of tableView that dims it when searching:
- (UIView *)blackOverlay {
if (!blackOverlay) {
blackOverlay = [[UIView alloc] initWithFrame:[self overlayFrame]];
blackOverlay.alpha = 0.75;
blackOverlay.backgroundColor = UIColor.blackColor;
[tableView insertSubview:blackOverlay aboveSubview:self.parentViewController.view];
}
return blackOverlay;
}
This works perfectly as long as tableView does not contain sections. When tableView does contain sections and the tableView updates (such as when the view reappears after popping a view off of the navigation controller stack), the section headers are rendered above blackOverlay. This leaves tableView dimmed except for the section headers. I've tried calling [tableView bringSubviewToFront:self.blackOverlay] from within viewWillAppear:, but I get the same behavior.
My current work-around is returning nil for tableView section headers while the overlay is present, but this leaves whitespace gaps in the overlaid tableView where the section headers were previously.
How can I insure that tableView section headers are never drawn above blackOverlay? Or, is it possible to create a view in front of tableView from within a UITableViewController subclass that is not a subview of tableView?
First off, your function should be tweaked a bit. If you're returning an object that was alloc'd but not autorelease'd, then your method name should indicate that (i.e. newBlackOverlay). Second, your method is modifying a tableView object that was not given to it, so its interactions with other components is not obvious (see Law of Demeter).
The problem is that you're putting this black overlay as a child of the table view. You should be inserting it at the same level of the table view, i.e.:
UIView (set to the view controller's view)
|
+-UITableView
|
+-UIView (your new black overlay)
You can create it inside Interface Builder and set the backgroundColor/alpha properties as you see fit. Create a new UIView #property in your view controller and set it to your new UIView. Then you can change the overlay's alpha value and/or hide it completely in your callback functions for when the user starts/ends searching tasks.
I have a UITableView whose data have
sections. I display an overlay view on
top of tableView that dims it when
searching
FWIW, in case you don't know, you can build search screens fairly easily with the new UISearchDisplayController class Apple introduced in iPhone OS 3.x. Might save you re-inventing the wheel, here.
I solved this problem by creating a root view controller and making my table view a subview of this controller:
- (void)viewDidLoad {
[super viewDidLoad];
// Create the root view to hold the table view and an overlay
UIView *root = [[UIView alloc] initWithFrame:self.navigationController.view.frame];
root.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
root.autoresizesSubviews = YES;
self.view = root;
// Setup the table view
UITableView *newTableView = [[UITableView alloc] initWithFrame:self.navigationController.view.frame style:UITableViewStylePlain];
newTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView = newTableView;
[newTableView release];
[root addSubview:self.tableView];
[root release];
}
I was then able to use a UISearchDisplayController with a search bar at the top of the tableView without issue.
Related
Is it possible to disable the scrolling of tableHeaderView (Not to be confused with section header).Right now whenever I scroll the table, view in the tableHeaderView also gets scrolled.
What i am doing:
I have a class subclassed from UITableViewController.
In storyboard, I am using the static table view.
Table style is Grouped and I have added 8 sections having a row each.
On the top of 1st section, added a view which is the tableHeaderView.
I want to disable the scrolling of view with title "Profile" when I scroll the table.
PS:
I know this is achievable if I subclassed my class from UIViewController instead of UITableViewController.
But I don't want to UIViewController because I am using storyboard for designing static cell and If I use UIViewController instead of UITableViewController then compiler throws a warning "Static table views are only valid when embedded in UITableViewController instances"
Please let me know which is the best approach to achieve this.Is it possible to disable the scrolling of tableHeader using my current approach or do I need to use UIViewController instead.
Just use an embed segue with a parent UIViewController consisting of a header view and a container view. Embed your UITableViewController in the container view. More specific steps in this answer.
If you want everything in UITableViewController, you can insert your own subview doing something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.header = [[UIView alloc] init];
self.header.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);
self.header.backgroundColor = [UIColor greenColor];
[self.tableView addSubview:self.header];
self.tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
}
and then manipulate the position of the view in scrollViewDidScroll and friends:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.header.transform = CGAffineTransformMakeTranslation(0, self.tableView.contentOffset.y);
}
I say "and friends" because you'd need to take care of the corner cases like scrollViewDidScrollToTop:. scrollViewDidScroll gets called in every display cycle during scrolling, so doing it this way looks flawless.
Timothy Moose was spot on. Here are the necessary changes for iOS8.
MonoTouch (C#)
// create the fixed header view
headerView = new UIView() {
Frame = new RectangleF(0,0,this.View.Frame.Width,44),
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
BackgroundColor = UIColor.DarkGray
};
// make it the top most layer
headerView.Layer.ZPosition = 1.0f;
// add directly to tableview, do not use TableViewHeader
TableView.AddSubview(headerView);
// TableView will start at the bottom of the nav bar
this.EdgesForExtendedLayout = UIRectEdge.None;
// move the content down the size of the header view
TableView.ContentInset = new UIEdgeInsets(headerView.Bounds.Height,0,0,0);
.....
[Export("scrollViewDidScroll:")]
public virtual void Scrolled(UIScrollView scrollView)
{
// Keeps header fixed, this is called in the displayLink layer so it wont skip.
if(headerView!=null) headerView.Transform = CGAffineTransform.MakeTranslation(0, TableView.ContentOffset.Y);
}
[Export ("scrollViewDidScrollToTop:")]
public virtual void ScrolledToTop (UIScrollView scrollView)
{
// Keeps header fixed, this is called in the displayLink layer so it wont skip.
if(headerView!=null) headerView.Transform = CGAffineTransform.MakeTranslation(0, TableView.ContentOffset.Y);
}
I got a view controller (lets call it MainViewContoller) that's present 3 different tables (one in a time), user can tap a segment control to switch between those tables.
To present those 3 tables, MainViewContoller has 3 other view controllers (A, B and C), each has a UITableView as a subview and handle it's own data.
When a MainViewContoller is loaded, it initiate controllers A, B and C, and add their tableViews to it's view:
- (void)viewDidLoad {
[super viewDidLoad];
ViewControllerA *vcA = [ViewControllerA alloc] init];
[self.view addSubview:vcA.view];
ViewControllerB *vcB = [ViewControllerB alloc] init];
[self.view addSubview:vcB.view];
ViewControllerC *vcC = [ViewControllerC alloc] init];
[self.view addSubview:vcC.view];
}
So for example when user tap the segment control and choose A, the MainViewContoller hide tables B and C, and unhide table A. Something like this:
if (userTapOnA) {
self.viewControllerA.tableView.hidden = NO;
self.viewControllerB.tableView.hidden = YES;
self.viewControllerC.tableView.hidden = YES;
}
The problem:
When user tap the status bar I want that the current visible table will scroll to top.
This behavior is pretty basic and one gets it for free when using a regular view controller, but as you can see my view controller is not regular.
I suppose that by using other controllers view as MainViewContoller view I break the default behavior, so my MainViewContoller doesn't handle the status bar tap.
Someone got an idea how to solve that?
This is directly from the UIScrollView header file:
/* When the user taps the status bar, the scroll view beneath the
touch which is closest to the status bar will be scrolled to top, but
only if its scrollsToTop property is YES, its delegate does not
return NO from shouldScrollViewScrollToTop, and it is not already at
the top. On iPhone, we execute this gesture only if there's one
on-screen scroll view with scrollsToTop == YES. If more than one is
found, none will be scrolled. */
#property(nonatomic) BOOL scrollsToTop; // default is YES.
So in your case, set all scrollsToTop to NO, except the one you want to enable at that particular moment.
You should register your nested controllers as child controllers.
[self addChildViewController:vcA];
[self addChildViewController:vcB];
[self addChildViewController:vcC];
I'm not sure if this will help to solve your issue, but that's the right way to do it.
I have added a subview over my UITableView using:
TransparentViewController *tvc =
[[TransparentViewController alloc]
initWithNibName:#"TransparentViewController" bundle:nil];
[self.view addSubview:tvc.view];
My Nib has a UIImageView in it that has some text and a transparent background.
When I load the detailView for the table for the first time I show the subview that gives a brief explanation of the information that you can see below the text. Works really well.
What i would like to do is alter the alpha of the underlying table so that it is dimmer but not affect the alpha of the overlay subview. If i use:
[self.view setAlpha:(CGFloat)];
It dims the overlay as well. I seem to be having a mental block.
Changing the alpha affects the subviews as well. Your tvc.view is a subview of self.view, so it is naturally going to be affected.
Why don't you try this: put another view in tvc.view and send this view to this view to the back.
(UIView*) back = [[UIView alloc] initWithFrame:CGFrameMake(...)];
back.backgroundColor = [UIColor grayColor]; // choose a color that you like;
back.alpha = 0.5; // whatever works for you
[tvc.view addSubview:back];
[tvc.view sendSubviewToBack:back];
Set the size and alpha of this new view to something you like. The table view will show through it to a limited extent, which may accomplish what you are trying to do.
Since this is part of our tvc view, it will appear when you show that view and go away when you hide that view.
I'm using a UISegmentedControl as the headerview of a tableview. Now I want to add loading view (a view defined by myself) only covering the table cells but not my headerview. How do I achieve this?
You can add this view to the cell you want:
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPathOfCell];
[cell addSubview:view];
The simplest way to add the loading view would be like this
// get the frame of your table header view
CGRect headerFrame = headerView.frame;
// construct a frame that is the screen minus the space for your header
CGRect loadingFrame = [[UIScreen mainScreen] applicationFrame];
loadingFrame.origin.y += headerFrame.size.height;
loadingFrame.size.height -= headerFrame.size.height;
// use that frame to create your loading view
MyLoadingView *loadingView = [[MyLoadingView alloc] initWithFrame:loadingFrame];
// add your view to the window *
[[headerView window] addSubview:loadingView];
* Adding the view to the window may not be the best thing for your design, but since I don't know any of the details of your view hierarchy, this is the way that will always work.
Caution: If you do not cover up the segmented control, and it is enabled, the user may click on it and change the state of the app when you aren't expecting it - like when you're trying to load something for them. Be sure that you can cancel this loading view if the user changes the state of the app.
Below shows the default position when you add a grouped table to a view? How do I push the entire grouped table down in the view?
(source: pessoal.org)
You can assign a transparent view with a fixed height to the tableHeaderView property of the tableView. This will push the table contents down by the height of the transparent view.
You can do this from your UITableViewController's viewDidLoad:
// force the table down 70 pixels
CGRect headerFrame = self.tableView.bounds;
headerFrame.size.height = 70;
UIView *header = [[UIView alloc] initWithFrame: headerFrame];
header.backgroundColor = [UIColor clearColor];
self.tableView.tableViewHeader = header;
[header release];
Look at the delagate to the UITableView.
You will find a property 'heightForHeaderInSection'.
For section 0 just make the header larger (default is 0) it will push the table down the view.
If you are moving the table down, you undoubtedly wish to use the space you gain to add UI elements.
At that point, consider building the page in IB. You can resize the table view to be where you like and put the UI elements above the table. You can use a UIViewController to manage the page and add the UITableViewDelegate/Datasource protocol methods so that you can wire the UITableView back to your view controller as a delegate... then you can also wire the other UI elements to the same view controller.
The simplest way to do it is probably just to modify the frame for the tableview. You'll need to get a reference to the tableview in your controller either through an IBOutlet or by finding the view in the view hierarchy OR you can change the frame in Interface Builder.
In code something like:
tableView.frame = CGRectMake(0.0, 200.0, 320.0, 280.0);
Would position the tableview down the screen and limit its height - the dimensions you use will be dependent on whether you had a tab bar on the view and things like that.
In interface build just select the tableview, then choose the Size inspector (the inspector tab with the ruler icon) and set the height and y offset to shift it down the view.