how can i put the ScrollView on ViewController - ios

I am trying to put the Scrollview on the viewcontroller? is it possible? and i also want to put the UIView on the ScrollView.
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(20, 60, 280, 200)];
scrollView.contentSize = CGSizeMake(280, 200);
scrollView.backgroundColor=[UIColor orangeColor];
scrollView.delegate = self;
[self.view addSubview:scrollView];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 280, 200)];
container.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:container];
In this code i tried to put the scrollview on the VC and also i tried to put View on the ScrollView.
rb1 = [[RadioButton alloc] initWithGroupId:#"first group" index:j];
rb1.frame = CGRectMake(20,dy,22,22);
[container addSubview:rb1];
[rb1 release];
label1 =[[UILabel alloc] initWithFrame:CGRectMake(50, dy, 60, 20)];
label1.backgroundColor = [UIColor clearColor];
label1.text = [answers objectAtIndex:j];
[container addSubview:label1];
and here i tried to put the a radio button on View.
I hope it is clear:) Sorry my bad english.Thanks.

Scroll occurs when the frame of a scroll view is less than your content size.
Your frame is having size (280,200) and you have provided same size to contentSize.
To make the scroll possible replace below line into your code.
scrollView.contentSize = CGSizeMake(300, 260);

Here you put the same content size as scroll size:
scrollView.contentSize = CGSizeMake(280, 200);
In content size you should enter the size of area you want to scroll. In your case scroll does not need to scroll because scrollable area is same as scrollView size.

try this to put the scrollview on the controller
#import <UIKit/UIKit.h>
#interface YourClass : UIViewController {
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#end
to put v/v try this
NSUInteger scrollContentCount = 0;
for (NSUInteger arrayIndex = 0;
arrayIndex < [contents count];
arrayIndex++) {
// set scorllview properties
CGRect frame;
frame.origin.x = self.mScrollView.frame.size.width * arrayIndex;
frame.origin.y = 0;
frame.size = self.mScrollView.frame.size;
myScrollView.autoresizingMask = YES;
// alloc - init PODetailsView Controller
myController = [[MyController alloc] initWithNibName:#"MyController" bundle:[NSBundle mainBundle]];
[myController.view setFrame:frame];
// add view in scroll view
[self.myScrollView addSubview:myController.view];
scrollContentCount = scrollContentCount + 1;
}
// set scroll content size
self.myScrollView.contentSize =
CGSizeMake(self.myScrollView.frame.size.width * scrollContentCount,
self.myScrollView.frame.size.height);
}

Related

iOS 8 Custom keyboard UIScrollview isn't working

I have tried to add UISchollView in custom keyboard view but it shows view but not scrolling.
here is my code. i have also tried self.view but its not working either.
UIScrollView *scrollview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
NSInteger viewcount= 8;
for (int i = 0; i <viewcount; i++)
{
CGFloat y = i * 50;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,50, 50)];
view.backgroundColor = (i%2==0)?[UIColor greenColor]:[UIColor blueColor];
[scrollview1 addSubview:view];
}
scrollview1.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
scrollview1.backgroundColor = [UIColor redColor];
scrollview1.pagingEnabled=YES;
[self.inputView addSubview:scrollview1];
UIScrollView will scroll if its contentSize is greater then its frame size. So try setting its contentSize property and also check if userInteraction is enabled or not.
Hope it will work.

UIScrollView setContentOffset will add a subview to the scrollview?

I happened to find that setContentOffset to a UIScrollView will cause the scrollView append a new view to its subviews. here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((320 - 261) / 2, 50, 261, 67)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.backgroundColor = [UIColor darkGrayColor];
for (int i = 0; i < 5; i ++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 87, 1, 87, 65)];
view.tag = i;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 30)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:#"%d", i];
[view addSubview:label];
view.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view];
}
scrollView.contentSize = CGSizeMake(87 * 5, 67);
NSLog(#"before scroll: count of subview is %d", scrollView.subviews.count);
CGPoint offset = CGPointMake(87, 0);
[scrollView setContentOffset:offset animated:YES];
NSLog(#"after scroll: count of subview is %d", scrollView.subviews.count);
[self.view addSubview:scrollView];
}
Before calling setContentOffset:offset, the number of subviews of the scrollView is 5. This is what I expect. After that, the number turns to be 6.
Is it working as designed? How to avoid the new subview appended?
The extra subview is the scroll indicator. If you check again in a later method, it will have gone away. Don't worry.
You can confirm this by hiding the scroll indicators (showsVerticalScrollIndicator and showsHorizontalScrollIndicator properties).
Don't try to assume things about the view hierarchy of UIKit classes. UIKit can, and does, add its own views to several things - see tableviews and their cells, a navigation controller's view and so on.

Remove previous UIView before creating another on

I have a method were I'm creating UIView and it works fine when I call it from IBAction. But when I call the same method again it draws another view on top and I believe it's a memory leak. How do I remove previous UIView before creating another one? Thanks!
- (int)showQuestionMethod:(int)number;
{
UIView *questionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
blue:0.93 alpha:1.0];
[self.view addSubview:questionView];
questionView.tag = questionNumber;
UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
questionLabel.text = question1;
[questionView addSubview:questionLabel];
CGRect frame = questionView.frame;
frame.size.height = questionHeight;
questionView.frame = frame;
questionView.tag = questionNumber;
return currentQuestion;
}
- (IBAction)nextQuestion:(id)sender {
[self showQuestionMethod:questionNumber];
}
Create a property which will let you reference a view:
#property(nonatomic, strong) UIView *questionView;
Then change your method to remove the old view and create a new one:
- (int)showQuestionMethod:(int)number;
{
// Remove the previous view.
[_questionView removeFromSuperview];
// Create a new view.
_questionView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
_questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
blue:0.93 alpha:1.0];
// Add the view.
[self.view addSubview:_questionView];
_questionView.tag = questionNumber;
UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
questionLabel.text = question1;
[_questionView addSubview:questionLabel];
CGRect frame = _questionView.frame;
frame.size.height = questionHeight;
_questionView.frame = frame;
_questionView.tag = questionNumber;
return currentQuestion;
}
The simplest way would be to keep a reference to the view you will want to remove (a private property would work nicely). You could add the code at bottom to the top of your controllers .m file:
#interface MyUIViewController ()
#property (nonatomic, strong) UIView* questionView;
#end
Then modify your method as follows:
- (int)showQuestionMethod:(int)number;
{
[self.questionView removeFromSuperview]
self.questionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
self.questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
blue:0.93 alpha:1.0];
[self.view addSubview:self.questionView];
self.questionView.tag = questionNumber;
UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
self.questionLabel.text = question1;
[self.questionView addSubview:questionLabel];
CGRect frame = questionView.frame;
frame.size.height = questionHeight;
self.questionView.frame = frame;
self.questionView.tag = questionNumber;
return currentQuestion;
}
use this before creating new view
[myview removeFromSuperview];
You can either keep a reference to the view by adding #property (strong, nonatomic) UIView *questionView; in your #interface declaration then call [_questionView removeFromSuperview]; to remove it.
Or, better yet, keep that reference to it then have a method to reload the info on it and animate it out/back into view (perhaps by fading out, reloading the data on it, then fading it back in). That way you aren't throwing away / recreating views all the time, and instead you're just reusing the same, already created view.
[self.questionView removeFromSuperview];
before adding the other view, remove this from the superview!
Why not reuse the view?
If you'd rather delete it every time and you don't want to keep a reference to it, you can always set (dirty but simple):
questionView.tag = SOME_CONST;
And then add in the beginning of your method:
[[self.view viewWithTag:SOME_CONST] removeFromSuperview];
I know this question has been answered, but i want to add a precaution that you before removing a UIView or anything else you should FIRST CHECK IF IT IS ALLOCATED. So the removal should be like:
// Make property first
#property (nonatomic, strong) UIView *questionView;
- (int)showQuestionMethod:(int)number;
{
// Check it it is allocated
if(questionView)
{
[questionView removeFromSuperView];
//[questionView release]; // Un-Comment it if NOT using ARC
questionView = nil;
}
// At this point you safe to create a new UIView.
questionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
blue:0.93 alpha:1.0];
[self.view addSubview:questionView];
questionView.tag = questionNumber;
UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
questionLabel.text = question1;
[questionView addSubview:questionLabel];
CGRect frame = questionView.frame;
frame.size.height = questionHeight;
questionView.frame = frame;
questionView.tag = questionNumber;
return currentQuestion;
}
Hope the snippet and comment inside makes sense.
Happy Coding!!!

How to create UIScrollview inside UIScrollview programmatically

Please tell me how to set scrollview inside a scrollview like nested process.
The following code works partially.
int x=10;
int y=10;
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(50,50);
y=y+95;
}
Now all I can see are 3 scrollviews and the others are hidden. How can I create main scroll to view so that the child scrollViews are not hidden?
You need to have an initial scrollView that you then put these scrollViews in.
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
Then change
[self.view addSubview:scrollview];
To
[mainScrollView addSubview: scrollView];
//I have created Two Scroll view programmatically this way
UIScrollView *scrollViewOuter = [[UIScrollView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 600.0f, 600.0f)];
scrollViewOuter.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
scrollViewOuter.contentSize = CGSizeMake(2000.0f, 2000.0f);
UIScrollView *scrollViewInner = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 200.0f)];
scrollViewInner.backgroundColor = [UIColor whiteColor];
scrollViewInner.contentSize = CGSizeMake(2000.0f, 2000.0f);
[scrollViewOuter addSubview:scrollViewInner];
[self.window addSubview:scrollViewOuter];
//You can change frame and use in your own way
Just create a parent scrollview that is big enough to hold the 5 smaller ones, then change this line:
[self.view addSubview:scrollview];
to
[parentScrollView addSubview:scrollview];

Scrollview content shows but not scrolling

I have a ViewController with a ScrollView containing bunch of content from IB but the scrollview isn't working.
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* configure a bunch of label, text ,images and views from IB
*/
UIScrollView* scrollView = (UIScrollView*)self.view;
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(320, 2000);
}
I can see my content up until the bottom but can't scroll. If it means anything, this ViewController in one of the tabs in a TabBar
For scroll view to scroll its Frame size must be smaller then its Content Size
You have added the scrollView FROM code programatically but looks like you are talking about adding it through Xcode's Storyboard or in nib file
IF you are adding it thought code you need to add following code
[self.view addSubView:scrollView];
Here is one example
#interface ImageScrollView ()
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIPageControl *pageControl;
#end
-(void) baseInit {
// Scroll View - Full frame width
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.frame;
_scrollView.delegate = self;
// ImageView - Full frame width
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
int i = 0;
int count = [imagesArray count];
for (i = 0; i < count; i++) {
UIImage *image = [UIImage imageNamed:imagesArray[i]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
[imageView setImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = i+1;
[_scrollView addSubview:imageView];
}
[_scrollView setContentSize:CGSizeMake(count * width, height)];
[_scrollView setFrame:CGRectMake(0, 0, width, height)];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
pageControl.numberOfPages = count;
pageControl.currentPage = 0;
CGRect pageFrame = CGRectMake(5, 5, 50, 50);
pageControl.frame = pageFrame;
pageControl.backgroundColor = [UIColor redColor];
[self addSubview:_scrollView];
[_scrollView addSubview:pageControl];
}
Dont forget to call baseInit function in your view did load.
This example work without needing to add any objects in Storyboard or nib files

Resources