I have created a UIView through code and want to add some images and description on that view.For that purpose i wished to add a scroll view so that we can user can scroll down to see the images and description.Can anyone help me how to add a scroll view above a UIView through code.Thanks in advance.
Hi try this:-
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[scrollView setDelegate:self];
[scrollView setShowsVerticalScrollIndicator:NO];
[scrollView setShowsHorizontalScrollIndicator:NO];
[[self view] addSubview:scrollView];
Now set the content size of scroll view:-
scrollView.contentSize = CGSizeMake(320, 600);//put values according to your requirements.
UIScrollView *newView = [[UIScrollView alloc] initWithFrame:scrollFrame];
[newView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[newView setDelegate:self];
[newView setBackgroundColor:backgroundColor];
[newView setPagingEnabled:YES];
[newView setShowsVerticalScrollIndicator:NO];
[newView setShowsHorizontalScrollIndicator:NO];
[[self view] addSubview:newView];
you can set Frame In place of scrollFrame.
cheers
Actually you probably don't want to create a UIView, you should create a UIScrollView then add the images and that as subviews of the UIScrollView. Then you'll have everything scrolling. You'll then set up the UIScrollView the way you want it to be.
Related
I have added few UI components using Storyboard (UILabel, UIImageView). I am trying to put UIScrollView in this view, below all other elements:
Here is my code right at the end of -viewWillAppear method (code from my View Controller):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
The reason why I need a UIScrollView is large amount of text in detailsLabel - I'm attaching code I use for handling this label programatically:
detailsLabel.text = #"Some long text taken from databse";
[detailsLabel sizeToFit];
detailsLabel.numberOfLines = 0;
detailsLabel.lineBreakMode = UILineBreakModeWordWrap;
What I am doing wrong? How do I put UIScrollView below all other elements of my main view?
Try this:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view insertSubview:scroll atIndex:0];
...
[self.view insertSubview:detailsLabel atIndex:1];
Put your code in ViewDidLoad . Then add the labels after you have added the UIScrollView to the self.View in the ViewDidLoad itself. So by default the UIScrollView would be added on the self.View and then your label. Now you can either add the label on the scrollview or onto the self.view .
I've followed the instructions in this answer and it worked great: I've changed the Custom Class in Identity Inspector from UIView to UIScrollView and added this line to my -viewDidLoad in the View Controller file:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 700)];
Option One:
Declare detailsLabel in .h:
IBOutlet UILabel * detailsLabel; // MAP IT WITH NIB or STORYBOARD
give it property and Synthesise in .m.
here in viewDidLoad declare:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
[scroll addSubview:detailsLabel];
// IT SHOULD WORK.
Option two:
If you have large amount of text to display, then you can also use UITextView, it has inbuilt scrollview. And you can also set its text edit property.
I need to add a Button or a label to a UIView and add it to my UIViewController. I did the following but it only crashed the program.
UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 300)] ;
[myview addSubview:myButton];
[self.view addSubview:myView];
The code crashes. Why is that?
You set your myview's width to 0.
You sure you created "myButton"?
You create myview but you add my*V*iew as subview. Is there any warning?
The first step you need to take is:
Make Sure you have allocated & initialized UIButton object with "initWithFrame" method.
provide a width to your UIView in "initWithframe" method, So that it can be seen.
Now use your [self.view addSubview:myView];
Please let me know if it is useful.
Use:
//Assuming that myview should have the size of the main view:
UIView *myview = [[UIView alloc] initWithFrame:self.view.bounds];
[myview addSubview:myButton]; //Whatever ...
[self.view addSubview:myview]; //There was a typo in this line
If this code block is in the init or loadView of viewcontroller, this will crash.
Make sure you have created a view for view controller in this case before adding views to self.view
UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = aView;
[aView release];
myButton = [[UIButton alloc] init....
.....
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 300)] ;
[myView addSubview:myButton];
[self.view addSubview:myView];
Try updating the view with the following:
[myview setNeedsDisplay];
and remove the second call to addSubView (which is probably causing the crash):
[self.view addSubview:myView];
Ok we all know that nesting an image view inside of a scroll view allows you to have an image that you can pinch and zoom. Pretty cool. I want to duplicate this (eventually quadruple) feature.
In IB I have setup my 2 scroll views side by side, and have nested imageViews inside of them and have hooked them up to the proper variables and everything.
Inside of my viewController viewDidLoad I do this:
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"monitor.JPG"]];
[scrollView addSubview:imageView3];
//[scrollView setFrame:CGRectMake(0, 0, 1024, 660)];
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.minimumZoomScale = 1;
scrollView1.maximumZoomScale = 5;
scrollView1.delegate = self;
[scrollView1 setScrollEnabled:YES];
imageView31 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"monitor.JPG"]];
[scrollView1 addSubview:imageView3];
It seems like this should run ok. But is what happens when I build in run is quite unusual. There is just a blank background for where my first scroll view should be and then the second scrollView has its image in there...
If I just do the code for the first scrollView, I get the image. So it's like I am somehow overwriting the first by including the second?
I really can't understand how or why this behavior is occurring. Maybe I am going about it all wrong, what am I missing? Does anyone have any ideas as to how to implement this multiple scrollView view?
Thanks
[scrollView addSubview:imageView3];
[scrollView1 addSubview:imageView3];
The second one should be imageView31
i want to place a list of uiviews instead of uiimageviews as a content in uiscrollview for my ipad app.i got images scrolling by using imageviews, but i need uiviews as a list of subviews that has to be scroll horizontally?i tried adding uiviews in uiscrollview through Interfacebuilder ,but while running the app i can see only one uiview where as all remaining views are not there. please help me out of this.
I tried it like that, still it's not showing the view, and also I tried programmatically for dimensions of UIScrollView and views inside it like the the code as below: –
scrollView.frame=CGRectMake(0, 604, 728,365);
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView1];
[subView1 release];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView2];
[subView2 release];
[scrollView release];
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView1];
[subView1 release];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView2];
[scrollView setContentSize:CGSizeMake(758+758, 365+365)]]
[subView2 release];
[scrollView release];
[scrollView setContentSize:CGSizeMake(758+758, 365+365)]]
you need to set the contentsize to be able to scroll and see all subviews.
You need to add UIView as an when needed. you need to use programmatic approach to add UIView. You should have a look at PageControl sample code provided by Apple. I hope you get your answer.
i tried with [758+758,365]; . now it is scrolling horizontally but only first view i can see. second view is not coming .
scrollView.frame=CGRectMake(0, 604, 728,365);
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
[scrollView setContentSize:CGSizeMake(1600,365)];
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
subView1.frame=scrollView.bounds;
[subView1 addSubview:myImageView];
[scrollView addSubview:subView1];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blackColor];
subView2.frame=scrollView.bounds;
[scrollView addSubview:subView2];
[subView1 release];
[subView2 release];
[scrollView release];
HI,
I develop an application in which I want to implement the splash screen, on that splash screen I want to bind the scrollView and UIImage. My code as follow,
-(void)splashAnimation{
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
//scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView = [[UIScrollView alloc] initWithFrame:[window bounds]];
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
[scrollView setDelegate:self];
//[scrollView release];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self splashAnimation];
[self initControllers];
[window addSubview:[mainTabBarController view]];
[window makeKeyAndVisible];
}
On my given code the one blank window comes up and stay on.
I want to on that blank screen bind my splash.png.
****The Above problem is solved****
My current code is
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
scrollView.maximumZoomScale = 4.0f;
scrollView.minimumZoomScale = 1.0f;
CGRect rect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:rect animated:YES];
[scrollView setDelegate:self];
[window addSubview:scrollView];
[window makeKeyAndVisible];
I want to zoom the particular part of scrollView.
RajB - for your zooming of the scrollView do this:
CGRect zoomRect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:zoomRect animated:YES];
Hope this helps!
You create a UIScrollView but never add it to the view hierarchy, therefore it will never get displayed. Call [window addSubview:scrollView], and then don't forget to release it.
If you are using a MainWindow.xib in your project, your window is created for you, you do not need to create your own.
Use [[UIScreen mainScreen] instead of CGRect(0, 0, 320, 420) <- I also believe you meant "480"
After setting up your splash animation, you call [window addSubview:[mainTabBarController view]]. Even after adding your scrollview as previously mentioned, this will then become the topmost and therefore visible view.
Delay the [window addSubview:[mainTabBarController view]] until after your splash animation completes.