Extending UIScrollView when it fills up with more content - objective-c

Ok, so i got a scrollview to load when an user logs in to my application. When the user is authenticated, i need to create a scrollview programmatically. To do so, i have the following code:
[scrollView removeFromSuperview];
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, self.view.frame.size.height)];
view.backgroundColor = [UIColor greenColor];
//-- get the y-coordinate of the view
CGFloat viewCenterY = self.view.center.y;
//--calculate how much visible space is left
CGFloat freeSpaceHeight = applicationFrame.size.height - keyboardBounds.size.height;
//-- calculate how much the scrollview must scroll
CGFloat scrollAmount = viewCenterY - freeSpaceHeight / 2.0;
if(scrollAmount < 0) scrollAmount = 0;
//set the new scrollsize contentview
view.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
//scroll the scrollview
[view setContentOffset:CGPointMake(0, scrollAmount) animated:YES];
for(int v = 0; v < 15; v++){
CGFloat yCoord = v * 100;
UILabel *mijnLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, yCoord, 200, 50)];
mijnLabel.text = #"1337";
mijnLabel.font = [UIFont fontWithName:#"Verdana" size:20];
mijnLabel.textColor = [UIColor redColor];
[view addSubview:mijnLabel];
[mijnLabel release];
}
[self.view addSubview:view];
[view release];
}
How do i extend my view according to the amount of labels i added? I also want to know how i can make my scrollview fill up the entire screen.. My scrollview is now only filling up the screen when in portrait mode.
This is the login screen that gets removed:
[scrollView removeFromSuperview];

you need to keep track of numbers of label that you are adding into the scroll view. suppose if your label's height is 50 then take a variable and make a total of all label's height and give a new frame to your scroll view. like :
view.frame = CGRectmake(0,0,applicationFrame.size.width,totalHeightOfLabel);
[self.view addSubview:view];
[view release];
hope this will give a idea...

Related

Horizontal UIScrollView with multiple textfields to display

I'm new to Xcode. I want to make a UIScrollView with multiple pages and each page having multiple UITextFields which vary on each page. I made UIScrollView with paging enabled. Now I'm stuck at displaying textfield's on scroll pages.
Here is my code so far:
//set the paging to yes
self.scrollview.pagingEnabled = YES;
// create 5 pages
NSUInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++)
{
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//create the sub view and allocate memory
myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//create a label and add to the sub view
CGRect myFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
textLabel = [[UILabel alloc] initWithFrame:myFrame];
textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
textLabel.textAlignment = NSTextAlignmentLeft;
[myView addSubview:textLabel];
//create a text field and add to the sub view
myFrame.origin.y += myFrame.size.height + 10.0f;
textField = [[UITextField alloc] initWithFrame:myFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = i+1;
[myView addSubview:textField];
//set the background to different color
//set the scroll view delegate to self so that we can listen for changes
self.scrollview.delegate = self;
//add the subview to the scroll view
[self.scrollview addSubview:myView];
}
//scroll horizontally
self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.scrollview.frame.size.height);
//we set the origin to the 1rd page
CGPoint scrollPoint = CGPointMake(self.view.frame.size.width * 0, 0);
//change the scroll view offset the the 1rd page so it will start from there
[scrollview setContentOffset:scrollPoint animated:YES];
[self.view addSubview:self.scrollview];
}
Use This code here textfieldCount is
for (int j=0; j < textFieldCount; j++)
{
myFrame.origin.y += myFrame.size.height + 10.0f*(j+1);
UITextField *textField = [[UITextField alloc] initWithFrame:myFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = j+1;
[myView addSubview:textField];
}
Hope you got your answer.

How to disable vertical scrolling in UIScrollView (Obj-C)

I want to disable vertical scrolling from my UIScrollView if possible.. My code is like below.. Working fine except users can scroll up and down which shouldn't be there I believe.. Thanks in advance..
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)];
scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height);
scroll.pagingEnabled = YES;
scroll.backgroundColor = [UIColor blackColor];
int xVal = 30;
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)];
UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)];
UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)];
testLabel2.backgroundColor = [UIColor clearColor];
testLabel2.text =#"Test1";
testLabel2.textColor = [UIColor whiteColor];
testLabel2.font = [UIFont boldSystemFontOfSize:12];
testLabel1.backgroundColor = [UIColor clearColor];
testLabel1.text =#"Test2";
testLabel1.textColor = [UIColor whiteColor];
testLabel1.font = [UIFont boldSystemFontOfSize:12];
testLabel3.backgroundColor = [UIColor clearColor];
testLabel3.text =#"Test3";
testLabel3.textColor = [UIColor whiteColor];
testLabel3.font = [UIFont boldSystemFontOfSize:12];
xVal += 120;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)];
view.backgroundColor = [UIColor blackColor];
xVal += 200;
[scroll addSubview:testLabel1];
[scroll addSubview:testLabel2];
[scroll addSubview:testLabel3];
[scroll addSubview:view];
}
[self.view addSubview:scroll];
In my situation, I was unable to get the height of the scrollview (due to autolayout, I wasn't able to get the height in viewDidLoad). You can add this to the delegate method.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}
you must set your scrollview content height to the scroll view height
CGSize scrollableSize = CGSizeMake(scrollableWidth, yourScrollViewHeight);
[myScrollView setContentSize:scrollableSize];
Here may be a possible duplicate
disabling vertical scrolling in UIScrollView
or you can also try this:
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1);
Assuming it's an iPhone app, so the screen resolution is 320×480 .
Now you are setting your scroll view's height as self.view.frame.size.height / 3 .
Here your view's height is actually taken as 460 and not 480 (20px for staus bar).
So when you add the other view as subview to your scroll view, its frame goes out of the scroll's content view. So you need to manage this while setting your frames/content size.
Let me know if this works for you.
There is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also.
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height);
You should do like this:
aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);
In your xml file there are two properties are available for scrollview are horizontal scroll and vertical scroll. as per your requirement you can check or uncheck and if you want to stop vertical or horizontal scroll then you have to make same content size of scrollview with height or width of scrollview respectively

Using subviews in scrollerview iPhone

I am trying to add uipickerview in uiscrollview, but it is not being shown properly, just a black square.
Further, I have to add 1 more uipickerview, some labels and some buttons.
The code I am using is as under:
-(void)loadView {
[super loadView];
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroller.pagingEnabled = YES;
NSInteger numberOfViews = 2;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.height;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
// awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroller addSubview:awesomeView];
[scroller addSubview:pView];
}
scroller.contentSize = CGSizeMake(self.view.frame.size.width , self.view.frame.size.height * numberOfViews);
[self.view addSubview:scroller];
}
what can be the issue?
Are you putting data in the picker view? I mean, if you just put the picker view without data on it you are not going to see anything on it.
You need to add the delegate and the datasource and put some data in your picker!
pView.delegate = self;
pView.datasource = self;
and then you have to implemente at least:
numberOfComponentsInPickerView:
numberOfRowsInComponent:
titleForRow:
make sure you are implementing uipickerviewdelegate methods. its normal that it will show just the black square because your pickerview cant find the data and thus does not display anything.
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
Change height to 216 .. UIPickerView has fixed height of 216 ..only.. you can't change it..

UIScrollView with UIButtons 'skipping' when finger drags outside of its superviews bounds

I have a scrollView set up as the titleView of a navigationBar. The contentView of the scrollView has 4 buttons in it, all laid out dynamically. The buttons need to cancel with drags and scrolling, so I've subclassed them to setEnabled:NO on themselves when touchesMoved.
When I try to scroll the scrollView, as soon as I pass over the bounds of the superview (i.e., passing the bottom or top bounds navigationBar), the scrollview 'skips' 8 or 11 pixels, depending on if its the top or the button. It has something to do with the UIButtons, because when I just use UILabels the problem doesn't persist.
Relevant code:
Scrollview Initialization:
menuWidth = 196;
rowHeight = 40;
rowMargin = self.frame.size.height - rowHeight;
//drop down menu stuff
self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.contentSize = CGSizeMake(self.frame.size.width, (self.frame.size.height)*choices.count);
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.clipsToBounds = YES;
scrollView.canCancelContentTouches = YES;
scrollView.delaysContentTouches = YES;
scrollView.delegate = self;
[self layoutMenu];
[self addSubview:scrollView];
[scrollView release];
Button initialization (-layoutMenu):
SKScrollableButton *tempButton;
NSString *text;
for (int i = 0; i < choices.count; i++) {
text = (NSString*)[choices objectAtIndex:i];
if (i == 0) {
text = [text stringByAppendingString:downArrow];
}
CGRect labelRect = CGRectMake(0, i*(rowHeight + rowMargin), menuWidth, rowHeight - 7);
tempButton = [[SKScrollableButton alloc] initWithFrame:labelRect];
[tempButton setTitle:text forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[tempButton addTarget:self action:#selector(buttonUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:tempButton];
[tempButton release];
}
No events get called on the buttons when the skipping happens, and only normal-seeming scrollViewDidScroll delegate calls happen on the scrollView.
I've tested and the skipping definitely happens when passing the bounds of the superview, not the bounds of the scrollView itself.

UIScrollView Help

Is there a way to put several UIViews In a UIScrollView in a tabbar page. If so, how would I do that? To make it simple, Imageine the homescreen icons on the springboard. Each page is a UIView. I want to basically put this on a tabbar page. Is this possible? How would I do it?
NOTE:
First try to accept the answers then only you will get lot of good answer from our seniors.....
first create the uiscrollview and then add that it to the tabbar... for creating the uiscrollview
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];