Update Navigation bar - iphone

All i m trying to do is to update navigation bar when user turns page over. Below is my code
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController {
contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument];
currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];
if (currentIndex == 0) {
return nil;
}
contentViewController.page = [modelArray objectAtIndex:currentIndex - 1];
[self updateNavigationBar];
return contentViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument];
//get the current page
currentIndex = [modelArray indexOfObject:[(ContentViewController *)viewController page]];
if (currentIndex == totalPages - 1) {
return nil;
}
contentViewController.page = [modelArray objectAtIndex:currentIndex + 1];
[self updateNavigationBar];
return contentViewController;
}
-(void) updateNavigationBar{
if (CGPDFDocumentGetNumberOfPages(PDFDocument) > 1) {
title = [NSString stringWithFormat:#"Page %u of %u", currentIndex - 1, CGPDFDocumentGetNumberOfPages(PDFDocument)];
} else if ((CGPDFDocumentGetNumberOfPages(PDFDocument) < 1) {
title = [NSString stringWithFormat:#"Page %u of %u", currentIndex + 1, CGPDFDocumentGetNumberOfPages(PDFDocument)];
}
But seems like it is not working at all i m able to turn page over but not able to update navigation bar.
and this is the code for navigation bar
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45)];
_navBar.tintColor = [UIColor colorWithRed:243.0/255.0 green:164.0/255.0 blue:0.0/255.0 alpha:1.0];
title = [[UINavigationItem alloc] initWithTitle:[NSString stringWithFormat:
#"Page %u of %u",
currentIndex,
CGPDFDocumentGetNumberOfPages(PDFDocument)]];
[_navBar pushNavigationItem:title animated:NO];
Please help.
Appreciate it.
Thanks.

updating the navigation bar is done by updating the navigation items:
_navBar.topItem.title = #"Custom title";
[_navBar pushNavigationItem:_navBar.topItem animated:NO];

Related

How to hide/Remove the search bar on Contact Picker

I am adding a contact picker in my app, however, I do not want the search functionality.
How to hide/Remove the search bar on Contact Picker (ABPeoplePickerNavigationController)?
static BOOL foundSearchBar = NO;
- (void)findSearchBar:(UIView*)parent mark:(NSString*)mark {
for( UIView* v in [parent subviews] ) {
//if( foundSearchBar ) return;
NSLog(#"%#%#",mark,NSStringFromClass([v class]));
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
v.hidden=YES;
// foundSearchBar = YES;
break;
}
if( [v isKindOfClass:[UITableView class]] ) {
CGRect temp =v.frame;
temp.origin.y=temp.origin.y-44;
temp.size.height=temp.size.height+44;
v.frame=temp;
//foundSearchBar = YES;
break;
}
[self findSearchBar:v mark:[mark stringByAppendingString:#"> "]];
}
}
call above method after picker is presented as below:
-(void)showPeoplePickerController
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty],[NSNumber numberWithInt:kABPersonAddressProperty],nil];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentViewController:picker animated:YES completion:nil];
[self findSearchBar:[picker view] mark:#"> "];
[picker release];
}
-(void)showAddressBook {
ABPeoplePickerNavigationController *addressBook = [[ABPeoplePickerNavigationController alloc] init];
[addressBook setPeoplePickerDelegate:self];
addressBook.delegate = self;
addressBook.navigationBar.topItem.title = #"iPhone Contacts";
UIView *view = addressBook.topViewController.view;
for (UIView *v in view.subviews) {
if ( [v isKindOfClass:[UITableView class]] ) {
CGRect temp = v.frame;
temp.origin.y = temp.origin.y - 44;
temp.size.height = temp.size.height + 44;
v.frame = temp;
}
}
[addressBook release];
}
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
UISearchDisplayController *searchDisplayController = navigationController.topViewController.searchDisplayController;
[searchDisplayController.searchBar setHidden:YES];
}
}

xcode : How to change view page with Flip effect on Button tap?

Below is the code of ModelController.m:
- (id)init
{
self = [super init];
if (self) {
// Create the data model.
DataHandler *handler = [DataHandler defaultDataHandler];
[handler syncData];
NSInteger tempTotal=1;//[[DataHandler defaultDataHandler] getTotalContents];
NSMutableDictionary *favs=[Settings getSetting:kFavList];
if(favs!=nil){
tempTotal=[favs count]+1;
}
NSInteger TotalItems=tempTotal;//tempTotal>=6?tempTotal:6;
NSInteger PageSize=6;
NSInteger PageNo= (TotalItems/PageSize);
if(TotalItems%PageSize!=0){
PageNo++;
}
self.TotalPage=PageNo;
NSInteger i=0,temp=PageSize;
self.pageArray=[[NSMutableDictionary alloc] initWithCapacity:PageNo];
for (i=0; i<PageNo; i++) {
temp=(PageNo==i+1 && TotalItems%PageSize!=0)? TotalItems%PageSize:PageSize;
[self.pageArray setObject:[NSString stringWithFormat:#"%d",temp] forKey:[NSString stringWithFormat:#"%d",i+1]];
}
//NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//_pageData = [[dateFormatter monthSymbols] copy];
self.pageData=[self.pageArray allKeys];
}
return self;
}
- (HomeViewController *)viewControllerAtIndex:(NSUInteger)index {
NSLog(#"viewControllerAtIndex is called..");
// Return the data view controller for the given index.
if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
return nil;
}
// Create a new view controller and pass suitable data.
HomeViewController *homeViewController = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
homeViewController = [[[HomeViewController alloc] initWithNibName:#"HomeViewController_iPhone" bundle:nil] autorelease];
} else {
homeViewController = [[[HomeViewController alloc] initWithNibName:#"HomeViewController_iPad" bundle:nil] autorelease];
}
homeViewController.dataObject = [self.pageData objectAtIndex:index];
homeViewController.PageItemsCount= [[self.pageArray valueForKey:[NSString stringWithFormat:#"%d",index+1]] integerValue];
homeViewController.TotalPage=self.TotalPage;
homeViewController.CurrentPage=index+1;
return homeViewController;
}
- (NSUInteger)indexOfViewController:(HomeViewController *)viewController
{
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return viewController.CurrentPage-1;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(HomeViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(HomeViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
This is working fine when I swipe the screen, but I also want to change screen on any button tap of main view page (in this case it's homeViewController.m), as it's code is very lengthy I didn't post code here.
can anyone tell me how to achieve this?
Thanks
Ashish
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:0.80];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view cache:NO];
[UIView commitAnimations];
//Addd this line on click event of button action
You could simply try and animate the view transform like this (for a vertical flip):
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
view.transform = CGAffineTransformMakeScale(1, -1);
}
completion:nil];
Thanks

How to set page mode to single mode from double mode

Can we change the spine location without changing orientations. Like for some views it is set to max and for some views it should be mid. Is it possible to active this functionality.
This is the code i am using, but don't know what to modify in it so that without changing orientation i can set spine location to max and set one page mode rather than two page mode. Actually is started with two page mode when app runs but later it requires to move to single page mode, that is not getting possible, it gives error that you are providing one view but than two required.
Can anyone guide for this.
-(id)initWithViewControllers:(NSMutableArray *)Controllers
{
self = [super initWithNibName:nil bundle:nil];
if(self)
{
self.VControllers = Controllers;
pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.view.frame = CGRectMake(12.0f, 10.0f, 748.0f, 985.0f);
self.pageController.delegate = self;
self.pageController.dataSource = self;
[self.pageController setViewControllers:[NSArray arrayWithObject:[VControllers objectAtIndex:0] ] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished)
{
}];
[self addChildViewController:self.pageController];
[self.view addSubview:self.pageController.view];
check=YES;
}
return self;
}
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
//Set the array with only 1 view controller
UIViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
//Important- Set the doubleSided property to NO.
self.pageController.doubleSided = NO;
//Return the spine location
return UIPageViewControllerSpineLocationMax;
}
else
{
NSArray *viewControllers = nil;
exampleViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
NSUInteger currentIndex = [self.VControllers indexOfObject:currentViewController];
if(currentIndex == 0 || currentIndex %2 == 0)
{
UIViewController *nextViewController = [self pageViewController:self.pageController viewControllerAfterViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
}
else
{
UIViewController *previousViewController = [self pageViewController:self.pageController viewControllerBeforeViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
}
//Now, set the viewControllers property of UIPageViewController
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
return UIPageViewControllerSpineLocationMid;
}
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
int index = [self.VControllers indexOfObject:viewController];
i=index;
//[self checkViewControllers];
if (index - 1 >= 0)
{
ind = index;
return [self.VControllers objectAtIndex:index - 1];
}
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
int index = [self.VControllers indexOfObject:viewController];
i=index;
if (index + 1 < [self.VControllers count])
{
ind = index;
return [self.VControllers objectAtIndex:index + 1];
}
if (ind1 + 1 < [self.VControllers count])
{
ind = index;
return [self.VControllers objectAtIndex:index + 1];
}
return nil;
}
If you want to change the spine location after the UIPageViewController is already initialized I think you may need to create a new one, with the new spine location, and replace the old one with it.
I guess the current implementation only considers setting the spine location while initializing or when changing orientations.
You may open a bug for Apple so they implement that in the future.

UIPageviewController - Change from two page mode to one page mode without changing orientation

I am using UIPageViewController to show two view controllers at a time. After showing a couple of such views, i want to show single view controllers without changing orientation, it is fixed to landscape. I am making in iPad.
Below is the code i am using for this:
-(id)initWithViewControllers:(NSMutableArray *)Controllers
{
self = [super initWithNibName:nil bundle:nil];
if(self)
{
self.VControllers = Controllers;
pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.view.frame = CGRectMake(12.0f, 10.0f, 748.0f, 985.0f);
self.pageController.delegate = self;
self.pageController.dataSource = self;
[self.pageController setViewControllers:[NSArray arrayWithObject:[VControllers objectAtIndex:0] ] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished)
{
}];
[self addChildViewController:self.pageController];
[self.view addSubview:self.pageController.view];
}
return self;
}
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
//Set the array with only 1 view controller
UIViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
//Important- Set the doubleSided property to NO.
self.pageController.doubleSided = NO;
//Return the spine location
return UIPageViewControllerSpineLocationMin;
}
else
{
NSArray *viewControllers = nil;
exampleViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
NSUInteger currentIndex = [self.VControllers indexOfObject:currentViewController];
if(currentIndex == 0 || currentIndex %2 == 0)
{
UIViewController *nextViewController = [self pageViewController:self.pageController viewControllerAfterViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
}
else
{
UIViewController *previousViewController = [self pageViewController:self.pageController viewControllerBeforeViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
}
//Now, set the viewControllers property of UIPageViewController
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
return UIPageViewControllerSpineLocationMid;
}
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
int index = [self.VControllers indexOfObject:viewController];
//[self checkViewControllers];
if (index - 1 >= 0)
{
ind = index;
return [self.VControllers objectAtIndex:index - 1];
}
ind = index;
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
int index = [self.VControllers indexOfObject:viewController];
//[self checkViewControllers];
if (index + 1 < [self.VControllers count])
{
ind = index;
return [self.VControllers objectAtIndex:index + 1];
}
ind = index;
return nil;
}
What i need is to show the single view controllers after shows double views a couple of time. This functionality i am unable to active yet. Can anyone guide for this .
Thanks in advance.

scrollView with pagecontrol programmatically?

I want to create scrollView with pagecontrol programmatically. How can i do that?
#pragma mark -
#pragma mark loadGalleryView
-(void) loadGalleryView{
galleryArr = [memberDic objectForKey:#"arrKey"];
if ([galleryArr count]%5 != 0)
{
noOfPages = ([galleryArr count]/5)+1;
}
else
{
noOfPages = [galleryArr count]/5;
}
viewControllers = [[NSMutableArray alloc] init];
for (int i=0; i<noOfPages; i++)
{
[viewControllers addObject:[NSNull null]];
}
[galleryScrollView setPagingEnabled:TRUE];
[galleryScrollView setContentSize:CGSizeMake(self.view.frame.size.width* noOfPages,69.0f)];
[galleryScrollView setShowsHorizontalScrollIndicator:FALSE];
[galleryScrollView setShowsVerticalScrollIndicator:FALSE];
[galleryScrollView setScrollsToTop:FALSE];
[galleryScrollView setDelegate:self];
[pageControl setNumberOfPages:noOfPages];
[pageControl setCurrentPage:0];
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
//-----------------Load scroll View----------------------------------
-(void) loadScrollViewWithPage:(int) page{
if (page < 0)
{
return;
}
if (page >= noOfPages)
{
return;
}
GalleryViewController *givc = [viewControllers objectAtIndex:page];
if ((NSNull *)givc == [NSNull null])
{
givc = [[GalleryViewController alloc] initWithPageNumber:page];
givc.imageArr = [galleryArr retain];
[viewControllers replaceObjectAtIndex:page withObject:givc];
[givc release];
}
if (nil == givc.view.superview)
{
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
givc.view.frame = frame;
[galleryScrollView addSubview:givc.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = galleryScrollView.frame.size.width;
int page = floor((galleryScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
isPageControlUsed = NO;
}
in gallary View Controller in .m file
- (void)viewDidLoad {
[super viewDidLoad];
float x = 7.0f;
for (int i = (pageNumber*5); i<(pageNumber+1)*5; i++)
{
if (i<[imageArr count])
{
NSString *url = [imageArr objectAtIndex:i];
MyImageView *imgView = [[MyImageView alloc] initWithFrame:CGRectMake(x, 7.5f, 55.0f, 55.0f)];
[imgView addImageFrom:url];
[self.view addSubview:imgView];
[imgView release];
x = x+62.5f;
}
}
}
-(id)initWithPageNumber:(int) page{
if (self = [super initWithNibName:#"GalleryViewController" bundle:nil])
{
pageNumber = page;
}
return self;
}