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)];
Related
can any body help me how to add two buttons to the navigation title view by using the title view property. I tried but, able to add only one button by using the following code.
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
customView.backgroundColor = [UIColor redColor];
UIButton *b1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[b1 setTitle:#"Hai" forState:UIControlStateNormal];
[b2 setTitle:#"Hello" forState:UIControlStateNormal];
[customView insertSubview:b1 atIndex:0];
[customView insertSubview:b2 atIndex:1];
self.navigationItem.titleView = customView;
use this code:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor redColor];
UIButton *b1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
[b1 setTitle:#"Hai" forState:UIControlStateNormal];
[b2 setTitle:#"Hello" forState:UIControlStateNormal];
[customView insertSubview:b1 atIndex:0];
[customView insertSubview:b2 atIndex:1];
self.navigationItem.titleView = customView;
Outputs:
It's because you have added the same frame for both Buttons. So, the Buttons are actually overlapped. You should use this code instead :
UIButton *b1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2=[[UIButton alloc]initWithFrame:CGRectMake(60, 0, 50, 50)];
Then you can simply add these buttons to your custom view.
[cusotmView addSubView:b1];
[cusotmView addSubView:b2];
Reason for Error :
You can't directly add UIButtons. You need to wrap them as UIBarButtonItems first.
Sample Code :
UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithCustomView:b1];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:b2];
[cusotmView addSubView:btn1];
[cusotmView addSubView:btn2];
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 a UIView subclass called BackgroundText to draw some text.
-(void) drawRect:(CGRect)rect
{
[#"synchronized." drawInRect:CGRectMake(0, 29, 320, 60) withFont:bigFont lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentRight];
}
--
backgroundText = [[MMRoomBackgroundText alloc] initWithFrame:CGRectMake (0, 142 + 44, 320, 80)];
[self.view addSubview:backgroundText];
I expect [backgroundText removeFromSuperview]; can remove these text from screen,but it doesn't work.
Thanks.
You need to call setNeedsDisplay on the view and check.
I hope this will help you.
Here LoadingBG is My View And LoadingText is My Label And I am Putting Text in Label and Add Label into View and After that I remove View From SuperView When I dont Need Text.
For Adding Text:
UIView * LoadingBG = [UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460);
UILabel *LoadingText = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)] autorelease];
LoadingText.text = #"Loading...";
LoadingText.backgroundColor = [UIColor clearColor];
[LoadingBG addSubview:LoadingText];
For Remove Text:
[LoadingBG removeFromSuperview];
And Here I had Use Fixed Size For View And Label. You can Use What You want as a frame.
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];
I have a UITextField and I want to center this field on the center of the view. Any ideas?
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];
Instead of CGRectMake(some numbers) - I want to place it in the center of the view.
You can directly assign it the center of the desired view:
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 200, 28)];
usernameField.center = myView.center;
userNameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 28)];
userNameField.center = CGPointMake(CGRectGetWidth(someView.bounds)/2.0f, CGRectGetHeight(someView.bounds)/2.0f);
Where someView is the superview of userNameField.
Why not use the view's center property?
userNameField.center = parentViewField.center;