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
}
Related
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 am trying to set search bar for table, so can anyone tell me that, is it possible to have uisearchabr when table scroll up / pull up and for other case search bar doesn't get appear on screen. Is it possible? If yes, then how?
Thank you in advance
You can implement scrollViewDelegate scrollViewDidScroll and check the contentOffset to show/hide search bar. Since table view is a subclass of UIScrollView, you can depend on this delegate method to decide when to show/hide it.
Basically when the contentOffset represents the scrolled up position, you can show this search bar and when the contentOffset value starts changing, you can hide the search bar. Use hidden property of the search bar to show/hide it. You can also use removeFromSuperView/addSubview methods.
You can use below code or concept.Concept is in scroll view delegate function you will check the content offset and display the set the search bar object in table view header.
Code :
//MARK: -UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (scrollView.contentOffset.y <= - 65.0f) {
[self.tableView setTableHeaderView:self.searchBar];
}
}
Make delegate of UIScrollViewDelegate and implement its delegate method,
in this when you start scroll the table then unhide your searchBar and change frame of tableView.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[mySearchBar setHidden:NO];
[quotesTableView setFrame:CGRectMake(0, 44, 320, 416)];
}
How do I create a hiding/unhiding nav bar like what pinterest and many other apps is doing? I know the basic idea is to use the UIScrollView delegate and detect whether I am scrolling up or down and show the nav bar based on that. So should I also adjust the navcontroller view height if the nav bar is hidden? How does this work?
I have a sample project located on github that does exactly the pinterest/piictu style 'hide the UINavigationController / UITabBarController stuff'
https://github.com/tonymillion/ExpandingView
I've tried https://github.com/tonymillion/ExpandingView and ran into a bunch of issues.
I ended up rolling my own navigation controller to get all the animations synced and used this scrollview code to figure out if I should expand or contract. iOS >=5.0
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
MyCustomNavController* navController = (MyCustomNavController*)self.parentViewController;
if( [scrollView.panGestureRecognizer translationInView:self.view].y < 0.0f ) {
[navController setExpanded:YES animated:YES];
} else if ([scrollView.panGestureRecognizer translationInView:self.view].y > 0.0f ) {
[navController setExpanded:NO animated:YES];
}
}
I would probably try to create my own root controller with scrollbar as main view and put navigation controller's view into it. You can't use scrollbar inside navbar view then but I believe you don't need it in this very case.
If this approach doesn't work I would probably create my own controller that mimic navigation controller appearance.
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 :)
I'm working on a browser app, and I have an address bar on top the UIWebView. On MobileSafari if you scroll down, the address bar starts to move to the top, out of the screen, and the UIWebView doesn't scroll. Only when the address bar disappears completely, it starts to scroll. I would like to have this effect in my app as well.
What's the best way to implement this?
Thanks
The only way to implement this requires iOS 5.
In iOS 5, UIWebView has an UIScrollView subview.
And use the following code:
Set a area for the address bar:
[[myWebView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
Move the address bar using the scrollview delegate:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if(scrollView.contentOffset.y>=-64&&scrollView.contentOffset.y<30)
{
topBar.frame=CGRectMake(0,-44-scrollView.contentOffset.y, 320, 44);
}
else if(scrollView.contentOffset.y<-64)
topBar.frame=CGRectMake(0,20, 320, 44);//Lock the position
}
There is a way, but I am not sure if it is a bit too hacky. First search for the scrollview within the webview, then alter the contentInset and finally add the searchbar(for example) to the scrollview. The following code is just an example, I did not set any frames correctly and 40 is just a made up height for the searchbar. I am not sure if this will work in every iOS Version.
UIWebView * myWebView = [[UIWebView alloc] init]
UISearchBar * mySearchBar = [[UISearchBar alloc] init];
for (NSObject * aSubView in [myWebView subviews]) {
if ([aSubView isKindOfClass:[UIScrollView class]]) {
UIScrollView * theScrollView = (UIScrollView *)aSubView;
theScrollView.contentInset = UIEdgeInsetsMake(40, 0, 0, 0);
[theScrollView addSubview:mySearchBar];
}
}
PeakJi's solution works but is a bit laggy. A better solution would be adding an observer to the UIScrollView's content offset, something like
[scrollview addObserver:self forKeyPath:#"contentOffset"
options:NSKeyValueObservingOptionNew context:nil];
You can find more document on NSKeyValueObserving protocol at
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html
Come to think of it, it is simply a scrolling view with an address bar stuck on the top, and both the web view and the bar always move together. Now, lets say you create a scroll view and add two subviews, the address bar and the web view (one below the other). It is to be noted that the height of the web view is determined and fixed after the page has been loaded (in webViewDidFinishLoad:).
Hence, it is simply a scrolling view whose contentSize is equal to the height of the bar + the height of the web view. Now, by default the web view allows scrolling, as it has a scroll view as a subview. As only the outer scroll view should be scrolling, it is required that the web view's scrolling be turned off. For that, fetch the first subview (that's the scroll view) and disable its scrolling using:
(UIScrollView*)[myWebView.subviews objectAtIndex:0].scrollEnabled = NO;