I'm wanting to add a UIScrollView with paging to go through different views from my existing view controller which is the root view of my app. I also have tab bar and navigation bar controllers along with it. Can I add a scroll view to this view controller to accomplish what I'm wanting, and if so, can someone point me in the right direction on how to go about it?
Here is my view controller.
#import "KFBViewController.h"
#import "ListViewController.h"
#import "ActionAlertsViewController.h"
#import "MarketUpdatesViewController.h"
#import "AgStoriesViewController.h"
#import "KFBNewsViewController.h"
#import "MemberBenefits.h"
#import "SocialNetworks.h"
#import "WebViewController.h"
#import "YouTubeView.h"
#import "KFBFlickrViewController.h"
#import "RSFM.h"
#import "UAPush.h"
#import "TUSafariActivity.h"
#interface KFBViewController ()
{
}
#end
#implementation KFBViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.title = NSLocalizedString(#"Home", #"Home");
self.tabBarItem.image = [UIImage imageNamed:#"home"];
self.navigationController.delegate = self;
}
return self;
}
- (void) showMenu
{
NSURL *urlToShare = [NSURL URLWithString:#"https://itunes.apple.com/us/app/kentucky-farm-bureau/id580530986?mt=8"];
NSArray *activityItems = #[urlToShare];
// TUSafariActivity *activity = [[TUSafariActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
- (IBAction)gotoSecondView
{
YouTubeView *youTubeView = [[YouTubeView alloc] initWithNibName:#"YouTubeView" bundle:nil];
youTubeView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:youTubeView animated:YES completion:nil];
}
- (IBAction)gotoPublicAffairs
{
ListViewController *publicAffairs = [[ListViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[publicAffairs setWebViewController:wvc];
publicAffairs.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:publicAffairs animated:YES];
}
- (IBAction)gotoActionAlerts
{
ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[actionAlerts setWebViewController:wvc];
actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:actionAlerts animated:YES];
}
- (IBAction)gotoMarketUpdates
{
MarketUpdatesViewController *marketUpdates = [[MarketUpdatesViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[marketUpdates setWebViewController:wvc];
marketUpdates.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:marketUpdates animated:YES];
}
- (IBAction)gotoAgStories
{
AgStoriesViewController *agStories = [[AgStoriesViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[agStories setWebViewController:wvc];
agStories.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:agStories animated:YES];
}
- (IBAction)gotoKFBNews
{
KFBNewsViewController *kfbNews = [[KFBNewsViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[kfbNews setWebViewController:wvc];
kfbNews.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:kfbNews animated:YES];
}
- (IBAction)gotoMemberBenefits
{
MemberBenefits *memberBenefits = [[MemberBenefits alloc] initWithNibName:nil bundle:nil];
memberBenefits.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:memberBenefits animated:YES];
}
-(IBAction)gotoPhotos:(id)sender
{
KFBFlickrViewController *photosView = [[KFBFlickrViewController alloc] initWithNibName:#"KFBFlickrViewController" bundle:nil];
[self.navigationController pushViewController:photosView animated:YES];
}
- (IBAction)gotoSocialNetworks
{
SocialNetworks *socialNetworks = [[SocialNetworks alloc] initWithNibName:nil bundle:nil];
socialNetworks.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:socialNetworks animated:YES];
}
- (IBAction)gotoFarmMarkets
{
RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil];
rsfm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:rsfm animated:YES];
}
- (IBAction)settingsButtonPressed:(id)sender
{
[UAPush openApnsSettings:self animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Home";
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:#"\u2699" style:UIBarButtonItemStyleBordered target:self action:#selector(settingsButtonPressed:)];
[settingsButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:24], UITextAttributeFont,nil] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = settingsButton;
UIBarButtonItem *systemAction = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showMenu)];
self.navigationItem.rightBarButtonItem = systemAction;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
#end
This works really well for me:
Declare a property for your UIScrollView, and set your ViewController as a UIScrollViewDelegate
#interface ViewController : UIViewController<UIScrollViewDelegate>
#property (strong, nonatomic) UIScrollView *theScrollView;
#end
**Note that I'm setting up my UIScrollView with code, but it can easily be done with a XIB.
In viewDidLoad: of your ViewController
self.theScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 300)];
self.theScrollView.delegate = self;
self.theScrollView.pagingEnabled = YES;
self.theScrollView.showsHorizontalScrollIndicator = NO;
self.theScrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:self.theScrollView];
NSArray *viewArray = [NSArray arrayWithObjects://your UIViews];
for(int i=0; i<viewArray.count; i++)
{
CGRect frame;
frame.origin.x = self.theScrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.theScrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
[subview addSubview:[viewArray objectAtIndex:i]];
[self.theScrollView addSubview:subview];
}
self.theScrollView.contentSize = CGSizeMake(self.theScrollView.frame.size.width * viewArray.count, self.theScrollView.frame.size.height);
Most paging scrollViews also have a UIPageControl associated with it. To do that, override this UIScrollViewDelegate method:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = self.theScrollView.frame.size.width;
int page = floor((self.theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.thePageControl.currentPage = page;
}
I would perhaps try using UIPageViewController, rather than a regular paging scrollview.
Apple's PhotoScroller sample code provides a very good example of how to use UIPageViewController.
I also have some code on github that modifies PhotoScroller to load the UIPageViewController inside a UIViewController subclass. (In Apple's sample code, the UIPageViewController is the root view controller.)
I have never used a UIPageViewController with different UIViewController subclasses in each page (the PhotoScroller sample code uses PhotoViewControllers in all pages), but I can't see why it couldn't be done with a few modifications to the code.
This link helps for you. Here page controller and scrollview both are used -
PageControl Example in iPhone
Related
I am developing IOS project using objective C.
I am going to make sliding tab page.
I found this source from github.
https://github.com/PageMenu/PageMenu
This is the source code which I use CAPSPageMenu.
When
#interface BusinessTabViewController ()
#property(nonatomic) CAPSPageMenu* pageMenu;
#end
#implementation BusinessTabViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *controllerArray = [NSMutableArray array];
UIViewController*controller = [[UIViewController alloc] initWithNibName:#"GongsiyingyeViewController" bundle:nil];
controller.title = #"Sample title";
[controllerArray addObject:controller];
NSDictionary *parameteres = #{CAPSPageMenuOptionMenuItemSeparatorWidth:#(4.3), CAPSPageMenuOptionUseMenuLikeSegmentedControl:#(YES), CAPSPageMenuOptionMenuItemSeparatorPercentageHeight:#(0.1)};
_pageMenu = [[CAPSPageMenu alloc] initWithViewControllers:controllerArray frame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) options:parameteres];
[self.view addSubview:_pageMenu.view];
}
It shows only white screen.
Please help.
Thanks.
The error is not related with PageMenu, Comment the code for Registering Push Notification then try it.
And Before pushing a viewcontroller check that viewcontroller is nil or not.
CAPSPageMenu is working fine in our project.
#interface BusinessTabViewController ()
#property(nonatomic) CAPSPageMenu* pageMenu;
#end
#implementation BusinessTabViewController
-(void) viewDidLayoutSubviews{
NSMutableArray *controllerArray = [NSMutableArray array];
UIViewController*controller = [[UIViewController alloc] initWithNibName:nil bundle:nil];
controller.title = #"Sample title1";
UIViewController*controller1 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
controller1.title = #"Sample title2";
UIViewController*controller2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
controller2.title = #"Sample title3";
[controllerArray addObject:controller];
[controllerArray addObject:controller1];
[controllerArray addObject:controller2];
NSDictionary *parameteres = #{
CAPSPageMenuOptionMenuItemSeparatorWidth:#(4.3),
CAPSPageMenuOptionUseMenuLikeSegmentedControl:#(YES),
CAPSPageMenuOptionMenuItemSeparatorPercentageHeight:#(0.1),
CAPSPageMenuOptionMenuHeight:#(40),
CAPSPageMenuOptionMenuMargin:#(20),
CAPSPageMenuOptionSelectionIndicatorHeight:#(2.0)
};
_pageMenu = [[CAPSPageMenu alloc] initWithViewControllers:controllerArray frame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) options:parameteres];
[self.view addSubview:_pageMenu.view];
}
I use viewDidLayoutSubviews() for get accurate the size of frame.
It is working fine.
How to set selected tab in objective c,
i am adding tab programmatically,
Here is my code for that:
#import "TabControllerViewController.h"
#import "DetailsView.h"
#interface TabControllerViewController (){
NSMutableArray *kirtanDetail;
CGPoint startPosition;
}
#end
#implementation TabControllerViewController
- (void)viewDidLoad {
[super viewDidLoad];
dbm=[[DBManager alloc]init];
[dbm openDB];
kirtanDetail=[dbm getAllPad:[NSString stringWithFormat:#"%#",_kirtanID]];
// Do any additional setup after loading the view.
[self addTabView:[kirtanDetail count]];
[self.tabBarController setDelegate:self];
self.tabBarController.selectedIndex = 2;
}
-(void)viewDidAppear:(BOOL)animated{
// [[NSNotificationCenter defaultCenter] postNotificationName:#"activeTab" object:#"2"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)addTabView:(NSInteger *) tab {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
for(int i=0;i<tab;i++){
UIViewController *view1 = [sb instantiateViewControllerWithIdentifier: #"DV"];
[view1 viewDidLoad];
[tabViewControllers addObject:view1];
//can't set this until after its added to the tab bar
view1.tabBarItem =[[UITabBarItem alloc] initWithTitle:[NSString stringWithFormat:#"PAD %d",i+1] image:[UIImage imageNamed:#"view1"]tag:i];
UITextView *textView=[[view1.view subviews]objectAtIndex:0 ];
NSArray *data=[kirtanDetail objectAtIndex:i];
if([[data objectAtIndex:0] isEqualToString:[NSString stringWithFormat:#"%#",_kirtanID]]){
NSLog(#"Active TAB=%d",i);
}
textView.text=[NSString stringWithFormat:#"%#",[dbm getKirtanDetails:[NSString stringWithFormat:#"%#",[data objectAtIndex:0]]]];
}
[self setViewControllers:tabViewControllers];
}
-(void)setActiveTab:(NSNotification *)notification{
[self.tabBarController setSelectedIndex:[notification object]];
}
#end
[self.tabBarController setSelectedIndex:1];
Add this after viewDidLoad, in viewWillAppear
EDIT:
Is this controller TabControllerViewController subclass of UITabBarController or UIVIewController? If it's subclass of UITabBarController then replace [self.tabBarController setSelectedIndex:1] with [self setSelectedindex:1]
I'm following these instructions per the ios docs on UIPageViewController and UIPageControl:
A page indicator will be visible if both methods are implemented, transition style is 'UIPageViewControllerTransitionStyleScroll', and navigation orientation is 'UIPageViewControllerNavigationOrientationHorizontal'. Both methods are called in response to a 'setViewControllers:...' call, but the presentation index is updated automatically in the case of gesture-driven navigation.
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
So I have implemented them as follows, inside a UIViewController wrapper class:
Here is the initialization:
#interface PageViewWrapperViewController : UIViewController <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
#property (strong, nonatomic) UIPageViewController *pageController;
#property (assign, nonatomic) NSInteger index;
#property (strong, nonatomic) UILabel *screenNumber;
#property UIPageControl *childPageControl;
#end
- (void)viewDidLoad {
[super viewDidLoad];
self.index = 0;
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
self.pageController.delegate = self;
[self.pageController.view setFrame:[[self view] bounds]] ;
UIViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
//[self.pageController didMoveToParentViewController: self];
self.childPageControl = [[UIPageControl alloc] init];
// self.childPageControl = [self getPageControlForPageViewController:self.pageController];
self.childPageControl.alpha = .5;
[self.view bringSubviewToFront:self.childPageControl];
}
Here are the required methods for the UIPageControl:
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return 5;
}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return self.index;
}
And here is where I give the PageController the individual view controllers:
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index{
if(index==0)
{
//JudgeViewController *next1 = [[JudgeViewController alloc] init];
JudgeTutorViewController *next1 = [[JudgeTutorViewController alloc] init];
next1.view.userInteractionEnabled = NO;
return next1;
}
else if(index==1)
{
FollowTableViewController *next1 = [[FollowTableViewController alloc] init];
next1.view.userInteractionEnabled = NO;
next1.typeDisplay = 1;
return next1;
}
else if (index==2)
{
StatTutorViewController *next1 = [[StatTutorViewController
alloc] init];
next1.view.userInteractionEnabled = NO;
next1.user = [[UserObject userUnique] copyUser].name;
return next1;
}
else if (index==3)
{
MenuTutorViewController *next1 = [[MenuTutorViewController
alloc] init];
next1.view.userInteractionEnabled = NO;
return next1;
}
else if(index ==4)
{
BuzzTutorViewController *next1 = [[BuzzTutorViewController alloc] init];
next1.view.userInteractionEnabled = NO;
return next1;
}
self.index = index;
return nil;
}
The UIPageViewController itself is working fine, but I don't see the UIPageControl. What is missing from my code?
Thank you for any suggestions.
Actually, neither of these answers is what the problem is. In fact, I didn't change my code but once I flushed my build settings a few times that did the trick. Thank you all!
self.childPageControl = [[UIPageControl alloc] init];
// self.childPageControl = [self getPageControlForPageViewController:self.pageController];
self.childPageControl.alpha = .5;
[self.view bringSubviewToFront:self.childPageControl];
At no point are you adding the page control as a subview. The view debugger in Xcode is a great way to spot missing UI elements like this.
I'm wondering how I would be able to design my own custom tabs, that when clicked could show mutiple view on the screen
http://tinypic.com/r/2cxtjk7/5
I'm only currently able to find the bottom tabbed bar approach. Thanks for your help im quite new still so if you could explain in detail that would be great :)
Show Multiple views using custom tabs
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
initWithNibName:#"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
initWithNibName:#"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
initWithNibName:#"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
initWithNibName:#"LocationsViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.selectedIndex = 0;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];
return self;
}
Like I said, this will display the selected controller properly, however when the app launches and I try to switch views with the tab bar, the subview just become grey... The following is the code to switch items:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == locationsTabBarItem) {
UIViewController *locationsController = [viewControllers objectAtIndex:0];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:locationsController.view];
self.selectedController = locationsController;
}
else if (item == accountsTabBarItem) {
UIViewController *accountsController = [viewControllers objectAtIndex:1];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:accountsController.view];
self.selectedController = accountsController;
}
else if (item == barTabTabBarItem) {
UIViewController *barTabController = [viewControllers objectAtIndex:2];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:barTabController.view];
self.selectedController = barTabController;
}
else {
UIViewController *moreController = [viewControllers objectAtIndex:3];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:moreController.view];
self.selectedController = moreController;
}
}
I added thing in view did load in the end here:
PatientDetailsViewController *patientDetailsView =
[[PatientDetailsViewController alloc]
initWithNibName:#"PatientDetailsViewController" bundle:nil];
[self addChildViewController:patientDetailsView];
[self.patientDetailsView addSubview:patientDetailsView.view];
[self.patientDetailsView setClipsToBounds:YES];
and then animated its opening with a button by adjusted the view frame size with an animation.
The code:
UIViewController *viewController = [[UIViewController alloc] initWithNibName:#"NibName" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.navigationBar.tintColor = [UIColor redColor];
self.popoverController = [[[UIPopoverController alloc]
initWithContentViewController:navigationController] autorelease];
popoverController.popoverContentSize = viewController.view.frame.size;
[popoverController presentPopoverFromRect:sender.frame inView:sender.superview
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[viewController release];
[navigationController release];
The tint color property of UINavigationBar doesn't work, it still has the default color.
What could I be doing wrong?
self.popoverController.popoverBackgroundViewClass = [MyCustomBackgroundView class];
#interface MyCustomBackgroundView : UIPopoverBackgroundView {
#private
}
#end
#implementation MyCustomBackgroundView
#synthesize arrowOffset;
#synthesize arrowDirection;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
+ (UIEdgeInsets)contentViewInsets {
return UIEdgeInsetsMake(0, 0, 1, 0);
}
+ (CGFloat)arrowHeight{
return 0.0;
}
+ (CGFloat)arrowBase{
return 0.0;
}
#end
As of iOS 5, popoverBackgroundViewClass is available which is the way to accomplish this.