display the view behind tableView (or scrollView) - ios

For example:
there is a tableView, when it is pulled down, the back view display. the effect is like this:
I tried like this but not working:
[self.view addSubview:logoView];
[self.view addSubview:tableView];

You have to
add your logo image view below the table view in xib.
set table view background color to clear color
Set logo image hidden.
in - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate or check for your self which delegate is best suited. set the logo image hidden to NO

You set the tableView background as your logoview. Keep your UITableviewCell as what color you want.

I'm not sure if i recognize your problem but i think you want something like this:
you'll have to use:
[self.view insertSubview:aboveSubview: ];
[self.view insertSubview:belowSubview: ];
not
[self.view addSubview: ];

You can achieve the same as the example image above by adding the view with a negative y offset.
UIView *header = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f - self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
header.backgroundColor = [UIColor lightGrayColor];
[self.tableView addSubview:header];
However if you have content in this view, such as a logo, it will appear to be pulled down from the top, rather than behind the table, I don't know if this is the effect you want or not.

Related

UITapGestureRecognizer only triggered for starting area of UIScrollView

I have several sub views added to a UIScrollView in the main view controller. Each sub view's view controller has its own tap recognizer. The problem is, only tapping in the starting area on the screen is recognised. After scrolling the view and tapping beyond the starting area, the tapping handler will no longer be called.
This SO post UIButton in non-visible area of UIScrollView seems to be relevant but its solution description wasn't really clear to me...
A simple report project could be found at: dropbox link
Thanks.
Please check your contentview size or setbackground color for scrollview and contentview. Contenview size is not increasing i think.
I have downloaded your project and see that the
SSubViewController *pvc = [SSubViewController controllerWithSubViewID:0];
pvc frame is out of bound
SSubViewController *pvc = [SSubViewController controllerWithSubViewID:0];
[self.scrollView addSubview:pvc.view];
[self addChildViewController:pvc];
[pvc.view setBackgroundColor:[UIColor grayColor]];
[pvc.view setFrame:CGRectMake(0, 0, 400, 200)];
[pvc didMoveToParentViewController:self];
SSubViewController *pvc1 = [SSubViewController controllerWithSubViewID:1];
[self.scrollView addSubview:pvc1.view];
[pvc1.view setBackgroundColor:[UIColor yellowColor]];
[self addChildViewController:pvc1];
[pvc1 didMoveToParentViewController:self];
[pvc1.view setFrame:CGRectMake(0, 400, 400, 200)];
Now see the Colours
As you can see Gray color, and Yellow color frame

Creating UIView in UITableViewController that doesn't moves with the table view

I would like to display a small UIView with some text in the center of a UITableViewController when I call the showHud method.
- (void) showHud {
UIView *paintView=[[UIView alloc]initWithFrame:CGRectMake(150, 150, 120, 30)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:paintView];
}
Actually the problem with this code is that the view doesn't stays on its place, when I scroll the tableview it goes up with cells. Is it possible to pin it somehow?
I don't think you can achieve that with a UITableViewController because the view property of such controller is a UITableView, so the content will always scroll with it.
You need to create a regular UIViewController and add a UITableView to that controller's main view. Then anything else you add to that main view, will stay in place as it is outside of the table view.
Try implementing scrollViewDidScroll delegate method and change the frame of paintView according to tableview offset.
eg:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset
[paintView setFrame:CGRectMake(150, 150+offset.y, 120, 30)];
}
But the best way would be to create a simple UIViewController.
You must use UItableViewController because of pullToRefresh controller?
UI could add a view to the window and position it to the very front:
UIView testView = [[UIIView alloc]initWithFrame:CGRectMake(10, [[UIScreen mainScreen]applicationFrame].size.height - 85 , 75, 75)]; [[[[[NSApplication sharedApplication] mainWindow] contentView] addSubview:testbox]; addSubview: testView]; testView.layer.zPosition = MAXFLOAT;
An UITableViewController is typically embedded in a "parent" view controller, like UINavigationController. Its UITableView will then have a superview, like UINavigationControllerWrapperView. Even if your table view controller is a root view controller, the table view's superview is UIWindow.
So you can safely assume that your table view always has a superview. Then add your UIView to this superview instead of the table itself:
//Note: frame should be different here as it's in the superview's coordinate system
UIView *paintView=[[UIView alloc]initWithFrame:CGRectMake(150, 150, 120, 30)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view.superview addSubview:paintView];

IOS 7 - Search Display Controller in TableView White Scrollview Background - Cannot change background color

In my app I'm using dark table and cells. I've set everything to black in my storyboard, and when it all works perfect until I add a Search Display Controller. The search box itself is the desired color, but when I pull down, I see a scroll view that is white. I cannot find anywhere in the storyboard settings to change that background color. I've checked several places to include searching this site in depth. Any help would be greatly appreciated.
I've gone as far as calling this helper I found on stackoverflow from viewDidLoad and programmatically setting all backgrounds to black. It still shows up as white. I've tried this for the self.view self.tableView self.searchDisplayController and still no dice.
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
[view setBackgroundColor:[UIColor blackColor]];
for (UIView *subview in subviews)
{
[subview setBackgroundColor:[UIColor blackColor]];
NSLog(#"%#, %#", [subview class], subview.backgroundColor);
[self listSubviewsOfView:subview];
}
}
Accidentally I found a solution to this problem. If you set the backgroundView property of the table view to anything, for example:
self.tableView.backgroundView = [[UIView alloc] init];
the white background disappears and backgroundColor is visible as expected.
Use this method .. it will call when you start searching..
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.backgroundColor = [UIColor anyColor];
}
You need to remember that while searching, another table view is loaded by the searchDisplayController.
So you may need to set the backgroundColor of the searchResultsTableView, like this:
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor purpleColor]]; //Purple couldn't be a good choice :)

UIViewController Methods viewDidLoad/viewWillHappen - view frame size detection

I have written some code which applies and image to a view for a UIViewController. The code is supposed to be iPhone screensize independent in as far as the difference in height between the iPhone 4 and 5.
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.clipsToBounds = YES;
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"myimage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
[self.view addSubview: imageView];
image = nil;
imageView = nil;
I found that when I add this code to the viewDidLoad method, the code failed to detect the different window size. However when I place it in viewWillAppear, the code does correct work with both screen sizes. I don't understand why.
Does any one know why this would happen ? I would like to understand it.
thanks
This happens because at the time the view is loaded, its' content hasn't necessarily been laid out and the size isn't known. This is especially true when using the autolayout system. The basic steps are,
The view is loaded
The view is laid out by the system using the constraints you give in the storyboard or code
The view appears
So the most appropriate place to put this appears to be viewDidLayoutSubviews. At that point the view and its subviews have been laid out, and the sizes are there. But putting it in viewWillAppear (or viewDidAppear, for that matter) will work, albeit will be less correct.
viewDidLoad method is called when the view controller's root view just has been created. At this point it is not added to the window hierarchy and auto layout has not been done. That is why you see this behavior.
This being said why draw the image in the graphic context? Just use UIImageView to display the image.
What you are doing is almost correct. As others have stated, in viewDidLoad the final size of the view controller's view is not set yet. The proper solution is to set the subview's autoresizingMask properly.
UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor colorWithPatternImage:image];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview: imageView];
This assumes you want the image view to fill the view controller's view.
Another solution is to update the frames of the subviews in the viewWillLayoutSubviews method.

How to also animate subview in navigation bar?

I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.

Resources