i added an image to the scroll view. but its not scrolling...
pls help out what is the problem..
Thanks.
boxImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 372)];
boxImage.image = [UIImage imageNamed:#"chapter1box.png"];
textScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 175, 320, 755)];
scrollTextImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 775)];
scrollTextImg.image = [UIImage imageNamed:#"chapter1narrationtext.png"];
textScroll.backgroundColor = [UIColor clearColor];
textScroll.scrollEnabled = YES;
textScroll.pagingEnabled = YES;
textScroll.directionalLockEnabled = YES;
textScroll.autoresizesSubviews = YES;
textScroll.contentSize = CGSizeMake(320, 775);
[self.view addSubview:boxImage];
[boxImage addSubview:textScroll];
[textScroll addSubview:scrollTextImg];
UIImageView has userInteractionEnabled property set to NO by default. Adding
boxImage.userInteractionEnabled = YES;
should help.
The scroll view size has to be smaller than the size of it's content for scrolling to work.
in the code i add the scrollview to an image view
i added it to the self.view its working now.
cant we add a scroll view to an imageview..?
Related
I have UITextView in my iPhone app. In the UITextView i have added UIImageView as subview. When the entered text reaches the final height the texts are scrolling to top. Instead the UIImageView (with image) also scrolling top. How can i handle to stop the image scroll and allow the text to scroll? Here is my code for your reference,
messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
messageTextView.delegate = self;
messageTextView.backgroundColor = [UIColor clearColor];
messageTextView.clipsToBounds = YES;
messageTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];
textViewImageView.image = [UIImage imageNamed: #"textbg.png"];
textViewImageView.contentMode = UIViewContentModeScaleToFill;
textViewImageView.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
[messageTextView addSubview: textViewImageView];
[messageTextView sendSubviewToBack: textViewImageView];
[messageTextView addSubview:textViewLabel];
Can anyone please help me to solve this? Thanks in advance. Looking forward your reply.
have you considered to position the imageView behind the textView? Or is there any reason that forbids such a layout?
something like that:
UITextView *messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:messageTextView];
UIImageView *textViewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
textViewImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:textViewImageView];
[self.view sendSubviewToBack:textViewImageView];
Maybe this could help.
Use the scrollview delegate to reposition your image when it reaches the bounds.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
CGRect imageFrame = textViewImageView.frame;
imageFrame.origin.y = scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height);
textViewImageView.frame = imageFrame;
}
}
This should work:
[yourTxtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"your image.png"]]];
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 am trying to add a UIView over the current view, but having no luck.. I am sure my code is correct but not sure what else to check, what else should I be thikning about when inserting subviews
this is what I have added into viewDidLoad
//..
cellContainer = [[UIView alloc] init];
cellContainer.frame = CGRectMake(0.0, 480.0, 320.0, 300.0);
[self.view addSubview:cellContainer];
self.view.backgroundColor = [UIColor greenColor];
//..
cellContainer is declared in the header as a UIView... any help would be appreciated.
cellContainer is likely getting added to the view hierarchy of of the UIViewController, but you've set the frame such that it is offscreen. Try this:
cellContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[self.view addSubview:cellContainer];
self.view.backgroundColor = [UIColor greenColor];
This will display cellContainer at the top left of the view, extending 320px in the right-x direction, and 300px in the down-y direction.
I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.
I just can't get my scroll view to actually scroll.. Everything displays fine except it just won't scroll..... Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
scrollView.contentSize = CGSizeMake(320, 600);
scrollView.scrollEnabled = YES;
scrollView.clipsToBounds = YES;
[self.view addSubview:scrollView];
planView = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView2 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView2.frame = CGRectMake(0, 164, 320, 165);
planView3 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView3.frame = CGRectMake(0, 328, 320, 165);
planView4 = [[WorkoutPlanView alloc] initWithImage:[UIImage imageNamed:#"WorkoutTable.png"]];
planView4.frame = CGRectMake(0, 505, 320, 165);
[scrollView addSubview:planView];
[scrollView addSubview:planView2];
[scrollView addSubview:planView3];
[scrollView addSubview:planView4];
}
How do I fix this problem?
The contentSize must be larger than the scrollview's frame for there to be anything to scroll. :)
In this case, they're both (320, 600) so change your scrollView's init to
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
your scrollview frame should be smaller than contentSize,so only you can make scrollview scrollable.