Why is UIScrollView not bouncing? - iphone

I do this:
scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
[self addSubview:scrollView];
NSInteger x = scrollView.bounds.origin.x + Padding, y = scrollView.bounds.origin.y + Padding;
backButton = [[AButtonControl alloc] initWithCaption:#"Back" style:AButtonViewStyleGreen];
backButton.frame = CGRectMake(x, y, backButton.frame.size.width, backButton.frame.size.height);
y += backButton.frame.size.height + Spacing;
[scrollView addSubview:backButton];
ownerNameTextField = [[AFormTextField alloc] initWithFrame:CGRectMake(x, y, 200, 60) label:#"Guest name"];
y += ownerNameTextField.frame.size.height + Spacing;
[scrollView addSubview:ownerNameTextField];
guestListSelectField = [[AFormSelectField alloc] initWithFrame:CGRectMake(x, y, 200, 60) label:#"Guest list"];
y += guestListSelectField.frame.size.height + Spacing;
[scrollView addSubview:guestListSelectField];
referenceUserSelectField = [[AFormSelectField alloc] initWithFrame:CGRectMake(x, y, 200, 60) label:#"Reference user"];
y += referenceUserSelectField.frame.size.height;
[scrollView addSubview:referenceUserSelectField];
scrollView.contentSize = CGSizeMake(self.bounds.size.width, y + Padding);
And it scrolls fine, but the view does not bounce like it is supposed(?) to.
Any ideas?

The underlying problem here is if you set the scrollview's frame/bounds in -layoutSubviews, it doesn't check if you're setting it to the frame/bounds that it already has, and this cancels the bounce. The solution is to do something like
CGFrame scrollFrame = [self calculateFrameForScrollView];
if (!CGRectEqualToRect(scrollFrame, self.myScrollView.frame))
self.myScrollView.frame = scrollFrame;
Found at: UIScrollView and its subclasses won't bounce if nested in custom UIView

Related

UITextView - Horizontal Scrolling?

How can I create a horizontal scrolling UITextView?
When I set my text programmatically it adds automatic line breaks, so I can only scroll vertically...
titleView.text = #"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
Thanks for your answers.
EDIT:
So far I tried this:
UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
yourScrollview.contentSize = CGSizeMake(textLength + 200, 500); //or some value you like, you may have to try this out a few times
titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);
[yourScrollview addSubview: titleView];
NSLog(#"%f", textLength);
but I received: 'Threat 1: signal SIGABRT'
I have not yet done something like this, but I would try the following steps to accomplish this:
Create a UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)]; //
Use CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
to get the final length of your text
Set yourScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times
Also set titleTextView.frame = CGRectMake(titleTextView.frame.origin.x, titleTextView.frame.origin.y, textLength, titleTextView.frame.size.height);
Make titleView a subview of yourScrollView: [yourScrollView addSubview: titleView];
Hope this gives you a good start!
EDIT: This Code will work:
Please notice I used a UILabel instead of a UITextView.
UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
titleView.text = #"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
titleView.font = [UIFont systemFontOfSize:18];
titleView.backgroundColor = [UIColor clearColor];
titleView.numberOfLines = 1;
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
myScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times
titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);
[myScrollView addSubview: titleView];
[self.view addSubview:myScrollView];
[titleView release];
[myScrollView release];

Zooming a UIScrollView containing a ViewController

I'm trying to zoom and before it was working with a image in the scrollview. Now I'm trying to replace that image with a ViewController and zoom in on the object in that ViewController. It is not working.
viewDidLoad:
- (void)viewDidLoad
{
self.boardView = [[BoardViewController alloc] init];
[self.scrollView addSubview:self.boardView.view];
[self.scrollView setContentSize:(CGSizeMake(320, 320))];
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
viewForZoomingInScrollView:
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [self.scrollView.subviews objectAtIndex:0];
doubleTapped method: (it is calling this when I put a log in it, it's just not zooming.
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
CGPoint pointInView = [recognizer locationInView:self.boardView.view];
CGFloat newZoomScale = self.scrollView.zoomScale * 3.0f;
if (self.scrollView.zoomScale > self.scrollView.minimumZoomScale){
newZoomScale = MIN(newZoomScale, self.scrollView.minimumZoomScale);
}
else {
newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
}
CGSize scrollViewSize = self.scrollView.bounds.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = pointInView.x - (w / 2.0f);
CGFloat y = pointInView.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrollView zoomToRect:rectToZoomTo animated:YES];
I don't understand what's wrong with it.

implement scrollview for subview iphone

How can I include this view in a scrollerView?
I have a subview with different textfields ,and I want to have it in a scrollview so that when a user clicks on a field view get scrolled.
- (UIView *)Form
{
if (!_Form) {
CGRect frame = CGRectMake(0.0, Height, Width, 310.0);
UIView *container = [[UIView alloc] initWithFrame:frame];
CGFloat y = 15.0;
frame = CGRectMake(15.0, y, width, height);
UITextField *field = [[UITextField alloc] initWithFrame:frame];
[
field.placeholder = #"text";
CGFloat spacing = 8.0;
y = frame.origin.y + height + spacing;
frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text6";
frame.size.height = 200.0;
y = frame.origin.y + height + space;
frame = CGRectMake(15.0, y, width - 2*15.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text1*";
y = frame.origin.y + height + 16.0;
CGFloat w = (kDeviceWidth - 2 * 15.0) / 2;
frame = CGRectMake(15.0, y, w - 2.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text3*";
frame = CGRectMake(15.0 + w + 2.0, y, w - 2.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text4*";
y = frame.origin.y + height + spacevalue;
frame = CGRectMake(15.0, y, w, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text5";
y = frame.origin.y + height + 20.0;
frame = CGRectMake((frame.size.width - 192.0) / 2, y, 192.0, 34.0);
y = frame.origin.y + height + 8.0;
frame = CGRectMake((frame.size.width - 192.0) / 2, y, 192.0, 34.0);
}
return _Form;
}
Can you help me with code pls.
Thanks
I have this code:
CGPoint puntoInicial;
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scrollView];
pt = rc.origin;
puntoInicial = pt;
pt.x = 0;
pt.y -= 250;
[scrollView setContentOffset:pt animated:YES];
textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
}
//Bajar la pantalla cuando se termina de editar
-(void) textFieldDidEndEditing:(UITextField *)textField {
puntoInicial.x = 0;
puntoInicial.y = 0;
[scrollView setContentOffset:puntoInicial animated:YES];
}
Only put this in your code and in the textField -> Connections inspector -> Oulets -> Delegate, put the Files Owner

How to create grid button on iphone. 10/10 matrix

How to create Grid of Buttons on iphone.
10/10 matrix...
I found NSMatrix for MAC ... Not for iphone...
any alternative way to create grid of button on my view.
#Thanks in advance.
Manually?
int rows = 10;
int cols = 10;
float gridWidth = 320.0;
float gridHeight = 320.0;
float buttonWidth = 28.0;
float buttonHeight = 28.0;
float gapHorizontal = (gridWidth - (buttonWidth * rows)) / (rows + 1);
float gapVertical = (gridHeight - (buttonHeight * cols)) / (cols + 1);
float offsetX;
float offsetY;
int count = 0;
do {
offsetX = gapHorizontal + ((count % rows) * (buttonWidth + gapHorizontal));
offsetY = gapVertical + ((count / rows) * (buttonHeight + gapVertical));
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(offsetX, offsetY, buttonWidth, buttonHeight)];
aView.backgroundColor = [UIColor redColor];
[self.view addSubview:aView];
[aView release];
offsetX+= buttonWidth + gapHorizontal;
count++;
} while(count < rows * cols);
Also check out AQGridView. I guess it's pretty much what you are looking for. And more.
As I usually use this to make grid.. Only you have to just replace with your row and column
#define MatrixRow 3
#define MatrixColumn 3
#define GRID_WIDTH 300
#define GRID_HEIGHT 300
UIView *grid = [[UIView alloc] initWithFrame:CGRectMake(10, 50, GRID_WIDTH, GRID_HEIGHT)];
grid.backgroundColor = [UIColor brownColor];
for (int i = 0; i< (MatrixColumn*MatrixRow); i++) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
view.backgroundColor = [UIColor yellowColor];
CGFloat xOrigin = GRID_WIDTH/(MatrixColumn*2) + (GRID_WIDTH/MatrixColumn)*(i%MatrixColumn);
CGFloat yOrigin = GRID_HEIGHT/(MatrixRow*2) + (GRID_HEIGHT/MatrixRow) *(i/MatrixColumn);
NSLog(#" (\"%.2f\" , \"%.2f\")",xOrigin,yOrigin);
view.center = CGPointMake(xOrigin, yOrigin);
[grid addSubview:view];
}
[self.view addSubview:grid];

UIScrollView offsetting content double the requested amount

The Issue
My PSWSnapshotView's end up exactly snapshotIndex too far to the right inside the _scrollView (40px offset). Yet, they are set with their frame to have the first one at (0, 0), though that first one shows up at (snapshotInset, 0) instead :/.
The Code
- (void)layoutViews
{
CGRect frame = self.frame;
CGFloat pageWidth = frame.size.width - (snapshotInset * 2);
CGRect pageFrame = CGRectMake(0.0f, 0.0f, pageWidth, frame.size.height);
NSInteger pageCount = [applications count];
[pageControl setNumberOfPages:pageCount];
[scrollView setFrame:CGRectMake(snapshotInset, 0.0f, pageWidth, frame.size.height)];
[scrollView setContentSize:CGSizeMake((pageWidth * pageCount) + 1.0f, frame.size.height)];
for (int i = 0; i < pageCount; i++) {
PSWSnapshotView *view;
if ([snapshotViews count] <= i)
{
PSWSnapshotView *view = [[PSWSnapshotView alloc] initWithFrame:pageFrame application:[applications objectAtIndex:i]];
view.delegate = self;
[scrollView addSubview:view];
[snapshotViews addObject:view];
[view release];
}
else
{
view = [snapshotViews objectAtIndex:i];
[view setFrame:pageFrame];
}
pageFrame.origin.x += pageWidth;
}
}
The Values
self.frame.size.width = 320;
self.frame.size.height = 370;
snapshotInset = 40;
[applications count] = 2;
All the sizes and frames are set correctly (I checked w/ NSLog).
Are you possibly setting the scrollView.contentOffset somewhere, independent of this? Also, and this may just be a copy/paste issue, you reference both snapshotInset and _snapshotInset.