how to make a toolbar side up Keyboard - objective-c

I have a toolbar and made this code to learn how to put it above the keyboard when I open it however after doing this or the keyboard appears.
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextView *textField;
#property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textField.delegate = self;
self.toolbar.delegate = self;
self.textField.inputAccessoryView = self.toolbar;
self.textField.inputView = self.toolbar;
[self.toolbar removeFromSuperview];
[self.textField becomeFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
self.textField.inputAccessoryView = self.toolbar;
}

Your line [self.toolbar removeFromSuperview]; is causing the toolbar to become nil, so it is not appearing.
And as #jammycoder said you only need to set inputAccessoryView of the text field. So do as he suggested and remove the line self.textField.inputView = self.toolbar;
Edit
I have setup a simple project and put a UIToolbar and a UITextView on the view controller, in the storyboard. Those two views are connected (IBOutlets) to the ViewController as you probably did,.
If I use your code it indeed does not show the keyboard, why? Because you are assigning the inputView of the UITextView.
So let's debug your code. First of all add bunch of NSLog statements like this.
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
NSLog(#"1 %#", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(#"2 %#", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputView = self.toolbar;
NSLog(#"3 %#", NSStringFromCGRect(self.toolbar.frame));
[self.toolbar removeFromSuperview];
NSLog(#"4 %#", NSStringFromCGRect(self.toolbar.frame));
[self.textField becomeFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(#"5 %#", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(#"6 %#", NSStringFromCGRect(self.toolbar.frame));
}
This will print something like:
2015-05-14 00:13:46.313 ToolbarKeyboard[8367:4583306] 1 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.314 ToolbarKeyboard[8367:4583306] 2 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.314 ToolbarKeyboard[8367:4583306] 3 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.315 ToolbarKeyboard[8367:4583306] 4 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.491 ToolbarKeyboard[8367:4583306] 5 {{0, 0}, {320, 0}}
2015-05-14 00:13:46.491 ToolbarKeyboard[8367:4583306] 6 {{0, 0}, {320, 0}}
So, we discovered that, somehow, your bar height became 0. And, to be honest I don't know why, but lets continue our debugging.
As pointed by #jammycoder you don't need to the the inputView of the UITextView you only need to do that if you want a custom keyboard. So lets change our code to be like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
NSLog(#"1 %#", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(#"2 %#", NSStringFromCGRect(self.toolbar.frame));
[self.toolbar removeFromSuperview];
NSLog(#"3 %#", NSStringFromCGRect(self.toolbar.frame));
[self.textField becomeFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(#"4 %#", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(#"5 %#", NSStringFromCGRect(self.toolbar.frame));
}
This will print that the height of the toolbar remained unchanged. And the toolbar is being shown above the keyboard.
Until here I was using a real device to debug, decided to use a simulator. Wait, it is not opening the keyboard there! It seems to be a new feature of Xcode 6, so I did a quick search and found this stackoverflow: Xcode 6: Keyboard does not show up in simulator Did what was saying in the most voted answer and the everything is working just fine.
So, in short. The following minimum code will produce the desired behavior. And if you are using the simulator to test/debug it make sure to do what is described here: https://stackoverflow.com/a/24497773/3927536
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextView *textField;
#property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.inputAccessoryView = self.toolbar;
[self.textField becomeFirstResponder];
}
#end

This line brings up the keyboard
[self.textField becomeFirstResponder];
When a textfield becomes first responder it automatically brings up the keyboard.
Edit: remove the following line
self.textField.inputView = self.toolbar;
You only need to set the toolbar as inputAccessoryView
Edit2: also remove this line
[self.toolbar removeFromSuperview];

Related

Passing data and delegation

I have got a question about delegation. I am fairly new to programming and I have just managed my first exercise-project in which I use delegation. All it is, is a Parent View Controller that calls a Child View Controller which has a text field and a button in it. I write something in that text field, press the button, the Child VC is dismissed and it passes the text back. It then gets displayed in a label on the Parent. I have got a couple of animations for the transition. It all works. Here are some pictures first and the code:
This is my parent view controller:
Click the button and the Child appears:
Write something in the text field and press the button to pass it back:
The data is displayed on a label in the parent view controller.
Here are the files for this:
ParentViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
#interface ParentViewController : UIViewController <ChildViewControllerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
ParentViewController.m
#implementation ParentViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)Pressed:(id)sender {
ChildViewController *childViewController = [[ChildViewController alloc]init];
[self displayContentController:childViewController];
childViewController.delegate = self;
}
- (void) displayContentController: (UIViewController*) content {
[self addChildViewController:content];
content.view.frame = CGRectMake(0, 115, 320, 240);
content.view.backgroundColor = [UIColor redColor];
CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[content.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void) passDataBack:(NSString *)data{
self.label.text = data;
}
#end
ChildViewController.h
#import <UIKit/UIKit.h>
#protocol ChildViewControllerDelegate <NSObject>
-(void)passDataBack:(NSString*)data;
#end
#interface ChildViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *txtfield;
#property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
- (IBAction)passingBack:(id)sender;
#property NSString *data;
#end
ChildViewController.m
#import "ChildViewController.h"
#interface ChildViewController ()
#end
#implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)passingBack:(id)sender {
NSString *itemToPassBack = self.txtfield.text;
[self.delegate passDataBack:itemToPassBack];
[self.childViewControllers lastObject];
[self willMoveToParentViewController:nil];
[UIView animateWithDuration:1
delay:0.0
usingSpringWithDamping:1
initialSpringVelocity:1
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.view.frame = CGRectMake(-320, 115, 320, 240);
} completion:^(BOOL complete){
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
}
#end
Regarding the code, there are a couple of things I am not sure about, as I am learning I copied a lot of code etc... so for example I am not sure as to how to animate the child controller back using the same method as I used to animate it in, or if it matters and what is best practice etc... also, I get a strange message in the console: "Unknown class ViewController in Interface Builder file." but it works, so I am not sure why it's there. For the record, I haven't used the storyboard, only xibs.
My question though is the following: I would like to have the button to dismiss the Child View Controller not in the Child View Controller but in the parent, like so:
And that's where I am lost. I just don't know how to trigger the method to pass data back from the Parent View Controller. Can anyone help? many thanks

Mirror text from UITextField on inputAccessoryView - UIToolBar to text on UITextField on navigationController.toolbar

In my app I have a UITextField on the navigationController toolbar.
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic,strong) NSArray *toolBarButtonItems;
#property (nonatomic,strong) UITextField *textField;
#property (nonatomic,strong) UITextField *textField2;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
self.textField.delegate = self;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.textField];
self.toolBarButtonItems = #[flexibleSpace,barButtonItem,flexibleSpace];
self.toolbarItems = self.toolBarButtonItems;
self.navigationController.toolbar.barTintColor = [UIColor blueColor];
[self.navigationController setToolbarHidden:NO animated:NO];
}
When the textField is clicked the keyboard opens up and I create a new inputAccessoryView toolbar with another textField.
-(UIToolbar *)addToolBar{
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:self.navigationController.toolbar.frame];
toolbar.barTintColor = [UIColor darkGrayColor];
self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
self.textField2.delegate = self;
self.textField2.borderStyle = UITextBorderStyleRoundedRect;
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.textField2];
[toolbar setItems:#[flexibleSpace,barButtonItem,flexibleSpace]];
return toolbar;
}
The idea is to change the firstResponder to the textField on the inputAccessoryView so this way I can see what I'm editing. The reason I am doing this is cause I can't scroll the Navigation toolbar up past the keyboard and I want to see the text that I am editing.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
textField.inputAccessoryView = [self addToolBar];
if(self.textField2.isFirstResponder != NO){
[self.textField2 becomeFirstResponder];
}
}
It doesn't seem to be working when I click on the textField in the navigationController toolbar. The new inputAccessoryView toolbar shows up over the keyboard but I can't edit the field because the responder doesn't seem to be changing. The return key doesn't work either. I have to hit it twice in order to close the keyboard and when I do the text doesn't match up between the two text fields.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
self.textField.text = self.textField2.text;
return YES;
}
I got it to work like this:
#import "KJMViewController.h"
#interface KJMViewController ()
#property (strong, nonatomic) UITextField *textField1;
#property (strong, nonatomic) UITextField *textField2;
#end
#implementation KJMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField1 = [[UITextField alloc]initWithFrame:CGRectMake(30, 7, 260, 30)];
self.textField1.borderStyle = UITextBorderStyleRoundedRect;
self.textField1.delegate = self;
UIToolbar *navToolbar = self.navigationController.toolbar;
[navToolbar addSubview:self.textField1];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(30, 7, 260, 30)];
self.textField2.borderStyle = UITextBorderStyleRoundedRect;
self.textField2.delegate = self;
[toolbar addSubview:self.textField2];
self.textField1.inputAccessoryView = toolbar;
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(firstRes:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)firstRes:(id)sender
{
[self.textField2 becomeFirstResponder];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.textField2) {
self.textField1.text = self.textField2.text;
}
[textField resignFirstResponder];
[self.textField1 resignFirstResponder];
return YES;
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]removeObserver:self forKeyPath:UIKeyboardDidShowNotification];
[super viewDidDisappear:animated];
}
#end
Here's what's happening in viewDidLoad:
Initialise toolbar and textField2
Set the inputAccessory for textField1 (the one hidden by the keyboard) here so it's ready to become firstResponder
Then in the viewDidAppear method:
Sign up for a notification that's sent when the keyboard is shown. You'll then write some code in the "firstRes" method to make textField2 the firstResponder. You need to make it the firstResponder using this notification because you know that it's in the view hierarchy by this time, which means it's able to become firstResponder. Calling it in the -(void)textFieldDidBeginEditing:(UITextField *)textField seems to fire it before textField2 comes on screen, meaning that it can't become firstResponder. We sign up for it in the viewDidAppear method because we only want to get the notification if we're on screen.
After textField2 resignsFirstResponder, textField1 becomes first responder again, so you have to call resignFirstResponder twice in the textFieldShouldReturn method.
Also, if we leave the screen, we need to remove ourself as an observer of the keyboard notification in the viewDidDisappear method.
Here's a link to the project I made in Xcode so you can see how it works:
https://github.com/kylejm/UIToolBar-UITextView

UIScrollView and still the keyboard hides the text

I have uiscrollview, 2 textviewfields and I read all the posts about keyboard hiding textview and tried many methods, but still it hides it.
My code is like this:
ViewController.h
#property(nonatomic,retain) IBOutlet UITextView *campaignTitle;
#property (weak, nonatomic) IBOutlet UITextView *campaignDescription;
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property UITextView *activeTextView;
ViewController.m
#synthesize campaignTitle;
#synthesize campaignDescription;
#synthesize scrollView;
#synthesize activeTextView;
#define SCROLLVIEW_CONTENT_HEIGHT 460
#define SCROLLVIEW_CONTENT_WIDTH 320
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
scrollView.contentSize=CGSizeMake(0, self.view.frame.origin.y+self.view.frame.size.height+50);
[super viewDidLoad];
}
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
textView.text = #"";
self.activeTextView = textView;
return YES;
}
- (BOOL) textViewShouldEndEditing:(UITextView *)textView
{
self.activeTextView = nil;
return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}
- (void)keyboardWasShown:(NSNotification *)notification
{
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// Step 3: Scroll the target text field into view.
CGRect aRect = self.view.frame;
aRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(aRect, activeTextView.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeTextView.frame.origin.y - (keyboardSize.height-15));
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void) keyboardWillHide:(NSNotification *)notification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (IBAction)dismissKeyboard:(id)sender
{
[self.activeTextView resignFirstResponder];
}
There is delegation from the scroll view and campaignTitle, campaignDescription.
What is wrong? why the keyboard still hiding the bottom uitextview? The extra code that you may see is to support Return button in UITextView
Or use TPKeyboardAvoiding.
It is working great, and is very easy to setup:
Add a UIScrollView into your view controller's xib
Set the scroll view's class to TPKeyboardAvoidingScrollView (still
in the xib, via the identity inspector)
Place all your controls within that scrollview
It also automatically hooks up "Next" buttons on the keyboard to switch through the text fields.
Make sure your UITextFields are actually inside of your UIScrollView inside your storyboard and not accidentally placed on the UIView below it.
setContentOffset should do SOMETHING, even if it is not the intended effect. Since it seems to be doing nothing, I'd suspect that either your UITextFields are not subviews of it or your UIScrollView is not wired up in interface builder.

iOS: UIImagePickerController Issue

I'm trying to use UIImagePickerController to grab a photo from the users Photos on their iPhone / iPad. This code works just fine for iPhone, but when I run it on iPad, the debugger gives me the message "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'". I'm very new to Objective-C, so I'm unsure of how to edit this code to use UIPopoverController when its being run on the iPad. I'd rather not create 2 new View Controllers, so I was wondering if someone knows what code I would need to add in to have it work on both the iPhone and iPad. Here is the code in my view controllers:
ViewController.h:
#interface PhotoViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIButton *grabButton;
UIImageView *image;
UIImagePickerController *imgPicker;
}
#property (strong, nonatomic) IBOutlet UIButton *grabButton;
#property (strong, nonatomic) IBOutlet UIImageView *image;
#property (strong, nonatomic) UIImagePickerController *imgPicker;
- (IBAction)grabImage;
#end
ViewController.m:
#import "PhotoViewController.h"
#implementation PhotoViewController
#synthesize grabButton;
#synthesize image;
#synthesize imgPicker;
- (IBAction)grabImage {
[self presentModalViewController:self.imgPicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Photo Gallery", #"Photo Gallery");
self.tabBarItem.image = [UIImage imageNamed:#"42-photos.png"];
}
return self;
}
- (void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[super viewDidLoad];
}
Thanks in advance!
You have the answer through that error message !
Use UIPopOverController with UIImagePicker for iPad.
If you want to know how to use UIPopOverController, you can look at this tutorial !
Another youtube tutorial - http://www.youtube.com/watch?v=6Gc3kxVwfmE
Like Legolas mentioned, in an iPad app you must use a UIPopOverController in order to present an imagePicker. I'm usually not a fan of having any code in my app that performs tasks based on device type, but if you don't find a better solution you can do the following.
if ([[UIDevice currentDevice].model isEqual:#"iPad"])
{
// Display image picker in a popover
}
else
{
// display imagePicker as a modal
}
Check the documentation for device models:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
It's worked for me. Please try below code
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imgpicker];
popover.delegate =self;
[popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}else{
[self presentModalViewController:self.imgpicker animated:YES];
}

Infinite UIScrollView not working on iOS 4.3

I try to build a endless scrolling UIScrollView. So far I took the apple sample "StreetScroller". So all I do is setting the contentOffset back when it reaches the end of the scroll view.
Override -layoutSubviews of the UIScrollView:
- (void)layoutSubviews
{
CGFloat contentWidth = [self contentSize].width;
CGPoint contentOffset = [self contentOffset];
CGFloat centerOffsetX = (contentWidth - [self bounds].size.width) / 2.0;
CGFloat distanceFromCenter = contentOffset.x - centerOffsetX;
if (ABS(distanceFromCenter) > (contentWidth / 4.0)) {
contentOffset = CGPointMake(centerOffsetX, contentOffset.y);
[super setContentOffset:contentOffset];
}
}
Now on iOS 5 this works like a charm. But on iOS 4.3 it's not working. As soon as I call [super setContentOffset:contentOffset] it stoops scrolling because next time -layoutSubviews get's called the [self contentOffset] does not return the contentOffset that was set.
I know there are a lot a questions about infinite UIScrollViews, but one of these has fixed this problem!
Try this One. This Code is Working Properly for me on iOS 4.3
RootViewController.h
#class ViewControllerForDuplicateEndCaps;
#interface RootViewController : UIViewController {
ViewControllerForDuplicateEndCaps *viewControllerForDuplicateEndCaps;
}
#property (nonatomic, retain) ViewControllerForDuplicateEndCaps *viewControllerForDuplicateEndCaps;
- (IBAction)loadScrollViewWithDuplicateEndCaps:(id)sender;
#end
RootViewController.m
#import "RootViewController.h"
#import "ViewControllerForDuplicateEndCaps.h"
#import "InfiniteScrollViewAppDelegate.h"
#implementation RootViewController
#synthesize viewControllerForDuplicateEndCaps;
- (IBAction)loadScrollViewWithDuplicateEndCaps:(id)sender {
InfiniteScrollViewAppDelegate *delegate = (InfiniteScrollViewAppDelegate*)[[UIApplication sharedApplication] delegate];
if(self.viewControllerForDuplicateEndCaps == nil) {
ViewControllerForDuplicateEndCaps *temp = [[ViewControllerForDuplicateEndCaps alloc] initWithNibName:#"ViewControllerForDuplicateEndCaps" bundle:nil];
self.viewControllerForDuplicateEndCaps = temp;
[temp release];
}
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[delegate.navigationController pushViewController:self.viewControllerForDuplicateEndCaps animated:YES];
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end
ViewControllerForDuplicateEndCaps.h
#import <UIKit/UIKit.h>
#interface ViewControllerForDuplicateEndCaps : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
- (void)addImageWithName:(NSString*)imageString atPosition:(int)position;
#end
ViewControllerForDuplicateEndCaps.m
#import "ViewControllerForDuplicateEndCaps.h"
#implementation ViewControllerForDuplicateEndCaps
#synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// add the last image (image4) into the first position
[self addImageWithName:#"image4.jpg" atPosition:0];
// add all of the images to the scroll view
for (int i = 1; i < 5; i++) {
[self addImageWithName:[NSString stringWithFormat:#"image%i.jpg",i] atPosition:i];
}
// add the first image (image1) into the last position
[self addImageWithName:#"image1.jpg" atPosition:5];
scrollView.contentSize = CGSizeMake(1920, 416);
[scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];
}
- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
// add image to scroll view
UIImage *image = [UIImage imageNamed:imageString];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(position*320, 0, 320, 416);
[scrollView addSubview:imageView];
[imageView release];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
NSLog(#"%f",scrollView.contentOffset.x);
// The key is repositioning without animation
if (scrollView.contentOffset.x == 0) {
// user is scrolling to the left from image 1 to image 4
// reposition offset to show image 4 that is on the right in the scroll view
[scrollView scrollRectToVisible:CGRectMake(1280,0,320,416) animated:NO];
}
else if (scrollView.contentOffset.x == 1600) {
// user is scrolling to the right from image 4 to image 1
// reposition offset to show image 1 that is on the left in the scroll view
[scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];
}
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end