Adding UITextView/UILabel on UIScrollView - iphone

I am trying to add a UITextView on UIScrollview. and then add the UIScrollView onto a UIView. The adding scrollView to UIView works however adding UITextView to a UISCrollView Does not work. Any pointers?
Thanks
UIScrollView * contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(300, 400, 250, 250)];
contentScrollView.backgroundColor = [UIColor whiteColor];
UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(300, 400, 200, 200)];
mainContent.text = #"HELLO";
mainContent.textColor = [UIColor blackColor];
[contentScrollView addSubview:mainContent];
[contentScrollView setUserInteractionEnabled:YES];
[contentView addSubview:contentScrollView];

your height of scrol view is 250 and width also 250. but u are giving the frame of textview beyond these limits i.e. 300, and 400. limit them to 250, 250 like
UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];

Use this Code:
UIScrollView * contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 320, 270)];
contentScrollView.backgroundColor = [UIColor whiteColor];
[contentScrollView setUserInteractionEnabled:YES];
UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(60, 30, 200, 200)];
mainContent.text = #"HELLO";
mainContent.textColor = [UIColor blackColor];
[contentScrollView addSubview:mainContent];
[self.view addSubview:contentScrollView];

Use this technique
UIScrollView * contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 320, 270)];
and add it into your UIScrollView like:
UITextView * mainContent = [[UITextView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
contentScrollView.backgroundColor = [UIColor whiteColor];
[contentScrollView setUserInteractionEnabled:YES];
[contentScrollView addSubview:mainContent];

Related

UIScrollView in iPhone?

I am working in iPhone application, Using UIScrollView to set in Horizontal, add 5 images in UIScrollView,and its working fine.
(I want to show 2nd image in opening time) Then i tried, when the screen shows, at the time 2nd image show. But i tried my level best, but i did not do that, please any one help me
Thanks in advance
I tried this:
UIScrollView *scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 45, 300, 360)];
scroll1.contentSize=CGSizeMake(2700, 360);
[scroll1 setShowsHorizontalScrollIndicator:NO];
scroll1.pagingEnabled=YES;
[self.view addSubview:scroll1];
UIImageView *SlideImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 300, 360)];
SlideImage.image=[UIImage imageNamed:#"img1.png"];
SlideImage.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage];
UIImageView *SlideImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(300,0, 300, 360)];
SlideImage1.image=[UIImage imageNamed:#"img2.png"];
SlideImage1.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage1];
UIImageView *SlideImage2 = [[UIImageView alloc]initWithFrame:CGRectMake(600,0, 300, 360)];
SlideImage2.image=[UIImage imageNamed:#"img3.png"];
SlideImage2.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage2];
UIImageView *SlideImage3 = [[UIImageView alloc]initWithFrame:CGRectMake(900,0, 300, 360)];
SlideImage3.image=[UIImage imageNamed:#"img4.png"];
SlideImage3.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage3];
UIImageView *SlideImage4 = [[UIImageView alloc]initWithFrame:CGRectMake(1200,0, 300, 360)];
SlideImage4.image=[UIImage imageNamed:#"img5.png"];
SlideImage4.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage4];
UIImageView *SlideImage5 = [[UIImageView alloc]initWithFrame:CGRectMake(1500,0, 300, 360)];
SlideImage5.image=[UIImage imageNamed:#"img6.png"];
SlideImage5.backgroundColor=[UIColor clearColor];
[scroll1 addSubview:SlideImage5];
Try this
[scroll1 setContentOffset:CGPointMake(300, 0) animated:FALSE];

UITextView subview UIImageView hiding entered text and scrolling with texts iPhone?

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"]]];

UITextView auto-complete bubble location

I am confused regarding auto-complete bubble location while entering data in UITextView.
What i have observed is that depending upon its frame's origin , auto-complete bubble either comes on top or bottom. There is no fixed location, provided scrollEnabled is set to NO.
Here are the two links. Code is written in init()
http://www.flickr.com/photos/26021742#N00/6975525835/
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 41)] autorelease];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
UITextView *growingTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 200, 27)];
growingTextView.font = [UIFont fontWithName:#"Helvetica" size:13];
growingTextView.scrollEnabled = NO;
[view addSubview:growingTextView];
http://www.flickr.com/photos/26021742#N00/6975525727/
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 41)] autorelease];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
UITextView *growingTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 200, 27)];
growingTextView.font = [UIFont fontWithName:#"Helvetica" size:13];
growingTextView.scrollEnabled = NO;
[view addSubview:growingTextView];
Can anyone explain this observed behavior ???
I know that popovers and bubbles will always automatically adjust it's position for ease of use. It's already on Apple's code and in both images the bubble position makes sense.

UIView subviews are not visible

I create an UIView in applicationDidFinishLaunchingWithOptions method:
CGRect inputTextViewRect = CGRectMake(0, 420, 320, 80);
UIView *inputTextView = [[UIView alloc] initWithFrame:inputTextViewRect];
inputTextView.backgroundColor = [UIColor grayColor];
Then I create two subviews - one UIButton and one UITextView:
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 450, 230, 20)];
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect buttonFrame = CGRectMake(250, 450, 50, 20);
My problem is if I add my inputTextField and sendButton as subviews to main window - they will be visible and working. If I add them to my UIView:
[inputTextView addSubview:inputTextField];
[inputTextView addSubview:sendButton];
And then add this view to main window:
[self.window addSubview:inputTextView];
In this case i see only gray background of inputTextView and no subviews that were added before.
Could you please help me figure out whats wrong with it?
The problem is with your CGRectMake call:
CGRectMake(x, y, width, height);
x and y and are coordinates where you want to place the element over the super view, so you have the following, which is incorrect:
CGRect inputTextViewRect = CGRectMake(0, 420, 320, 80);
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 450, 230, 20)];
CGRect buttonFrame = CGRectMake(250, 450, 50, 20);
Try changing those to
CGRect inputTextViewRect = CGRectMake(0, 0, 320, 80);
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 230, 20)];
CGRect buttonFrame = CGRectMake(0, 0, 50, 20);
After changing this you will be able to see UITextView and UIButton, then you make the x and y position changes according to your need.
Actually when you add inputTextField subview of inputTextView, it will be placed at y position of 450 inside inputTextView. But that itself is of height 80 only.
So when you add that into inputTextView, make the origin y position to 30.
So, if you add to main view, it will be like
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 450, 230, 20)];
Else if you add it to inputTextView, then
UITextView *inputTextField = [[UITextView alloc] initWithFrame:CGRectMake(10, 30, 230, 20)];

iPhone SDK: UIScrollView does not scroll

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.