Scroll to top when tapping the status bar - iphone

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.

Related

Tabbar Controller with swipe effect

I'm trying to do a Tabbar Controller like below effect:
By swiping an viewcontroller will redirect to next tab. How can we achieve this in iOS? Is there any other controls to do like this?
Just add UISwipeGestureRecognizer to your tabBarView controller and change your tabBar index after swipe.
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(swipeMethod:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeRecognizer];
And my method to handle swipe is :
-(void)swipeMethod: (UISwipeGestureRecognizer *) sender
{
NSLog(#"Swipe!");
}
EDIT
Or you can use UIScrollView with paging enable and UIView to display your data.
Here is the tutorial you are looking for Tabbar Controller with swipte effect
There is a library for this on GitHub, it is called MGSwipeTabBarController and is designed to do exactly what you are looking for.
It is as simple as :
NSArray *viewControllers = . . . //your view controllers
MGSwipeTabBarController *swipeController = [[MGSwipeTabBarController alloc] initWithViewControllers:viewControllers];
Please Note that it is only compatible with iOS7 and + and that you'll still need to design your own tab bar that respond to scroll events using the MGSwipeTabBarControllerDelegateprotocol.
https://github.com/mglagola/MGSwipeTabBarController
https://github.com/nicklockwood/SwipeView
you can use this class to achieve your goal...
or else you have to make animation for tap on tabbar using following method,
[UIView transitionFromView:<#(UIView *)#> toView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> completion:<#^(BOOL finished)completion#>]
If anyone is still looking you can find another implementation here on this youtube series
https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj
Edit:
So to my knowledge and according to the video, the idea is that you'll want to use two UICollectionViews in one view controller. One collection view to display the content (apps) and the other for the horizontal navigation containing the categories.
To create the green 'highlight' bar you can use a UIView and adjust the height/width of the bar using constraints - heightAnchor/widthAnchor and add that to the navigation bar.
To get the bar moving with the distance the user will swipe, there is a method you can override to capture the horizontal scrolling called scrollViewDidScroll. From here you'll want to provide a variable from your the UIView's constraint (of type NSLayoutConstraint) to be able to update the x position of the UIView
override func scrollViewDidScroll(_ scrollView: UIScrollView)
{
print(scrollView.contentOffset.x)
menuBar.myGreenBarLeftConstraint.constant = scrollView.contentOffset.x / 4
//or however many categories you have
}

Tab Bar controller is not accessible

I have created a tab based application for iphone. when the 1st tab presses a first view will present. this view contains a button, on pressing it another view loads.
Code is:
-(IBAction)buttonPressed: (id) sender
{
Cities *cv=[[Cities alloc] initWithNibName:#"Cities" bundle:nil];
cv.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:cv animated:YES];
[cv release];
}
Now problem is that this view is loading in whole screen so that I am not able to access tab bar.
I have set the frame for this view and the view is loading in this frame,
-(void)viewWillAppear:(BOOL)animated
{
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 400);
}
but in remaining part white screen is appearing means tab bar is not accessible.
I want that whatever will be load at any time tab bar should be always accessible.
Please help me out.
Add
cv.modalPresentationStyle = UIModalPresentationCurrentContext;
Have you tried using UINavigationController inside your tabbar to dig inside your UIViewControllers??
for Ref : Adding NavigationController to Tabbar
do you really need a viewController Class for what you are trying to display??
if der's no core functionality being used, i think it will be much easier with UIView.
Happy Coding :)

Nested ViewControllers and their memory imprint? Improvements if this is very bad?

#Jonah pointed me to the following explanation he has provided:
Question on View Controller's view
I currently have a MainViewController whose main(root?) view contains an Add Record UIButton which is linked to the following method and is dependent on which page the user currently has open in a UIScrollView:
#pragma mark -
#pragma mark Add View Support
- (void)add:(id)sender {
MyAddViewController *controller = nil;
// Dynamically creating and allocating the Add View Controller
if (dataViewPageControl.currentPage == 0) {
controller = [[MyAddViewController alloc] initWithAddType:1];
} else {
controller = [[MyAddViewController alloc] initWithAddType:0];
}
controller.delegate = self;
// Slide the new AddViewController's view in from the right
CGPoint initialCenter = self.view.center;
controller.view.center = CGPointMake(initialCenter.x + 320, initialCenter.y);
[self.view addSubview:controller.view];
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.35];
[UIView setAnimationDelegate:controller];
controller.view.center = CGPointMake(initialCenter.x, initialCenter.y);
[UIView commitAnimations];
}
MainViewController is the delegate for MyAddViewController (which has a CANCEL/CLOSE button) and when it's close button is clicked the MyAddViewController is released through the close method in this MainViewController.
As you can see, MyAddViewController's view becomes a subview to the current root view (Bad? Bad? Bad?) MyAddViewController's view is as a FULL size view (320x416) but whose top half is transparent, so that MainViewController's view can be seen below on the top half and the bottom half contains the AddViewController's view and textfields etc....
Based on what I've read from #Jonah just now at the link at the very top this sort of design is BAD, BAD, BAD. I've noticed when testing in Instruments that MyAddViewController sometimes lags when sliding in. I'll click the ADD button to fire this method and there is sometimes a 3 - 5 sec delay before the view slides in. Some sort of delay in all the memory management and cleanup?
I'm assuming I can fix this by making a single MyAddViewController property inside MainViewController which will then prevent me from allocating and deallocating on every button click. That should make things smoother? My only worry is that this will cause a greater memory imprint at application load time. Instruments seems to show a 516KB allocation every time I click ADD for the first time in my app.
Any recommendations though on design and how to improve this? The reason I like having separate view controllers with nested views like this is because MyAddViewController has a lot of logic with Core Data and such and MainViewController is pretty busy enough already as it is. Trying to keep things modular.
Thanks in advance!
I think the best solution is to redesign your MyAddViewController to be a controller which inherits from NSObject rather than UIViewController. The MyAddViewController can then manage a subview of your MainViewController allowing you to keep your controller logic nicely encapsulated without abusing UIViewController.
I've tried to describe why nesting the views of multiple custom UIViewController classes is problematic here: http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/
An add view controller is the perfect type of thing to use modal presentation for. Instead of handling all that animation yourself, just present it like this:
[self presentModalViewController:controller animated:YES];
Edit
Then I would recommend making MyAddViewController a property on MainViewController and write a custom accessor. Additionally I would defer setting it's addType (if possible) until the point at which you will actually animate the view in. This way we can just have one generic MyAddViewController hanging around, instead of having to create a new one every time the detailPageControl changes.
- (MyAddViewController *)addViewController
{
if (!addViewController)
addViewController = [[MyAddViewController alloc] init];
}
Then before you animate it...
self.addViewController.addType = 0;
if (dataViewPageControl.currentPage == 0) {
self.addViewController.addType = 1;
}

Change size of view

I put a button on view and want to call this view from another view and change size of this view but when i change size,size of button doesn't change How can i change size of view that size of all controls on view changed?
Three *second = [[Three alloc] initWithNibName:#"Three" bundle:nil];
second.view.frame=CGRectMake(0, 0, 100, 100);
[self.view addSubview:second.view];
When the time comes to change the size of the view, at the following code.
second.view.frame = CGRectMake(a,b,c,d);
Where a, b, c, and d are integers.
Set the frame for the view in the Interface Builder, you're using a nib anyway.
But I think it's really strange what you made. You create a UIViewController and set the view of this controller as a subview of another controller's view. Why don't you directly compose that view in the same nib?

UITableView section headers drawing above front subview

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.