How to make a UIScrollView with a UITextView sub view not scroll when adding text? - objective-c

I have a UIScrollView which has a subview UITextView. When I added text to UITextView by code, the view is scrolling to the last line. I want the view not to scroll when I add text to UITextView by code. Anyone can help me fix the problem?

Finally I can fix my problem.
We just set YES the setScrollEnabled in UITextView before we change the text in UITextView and set NO after we change the text.
Here the example code
[yourTextView setScrollEnabled:YES];
yourTextView.text = [someString copy];
[yourTextView setScrollEnabled:NO];
:)

Related

UIView in UITableView disappear when becomes firstResponder

In storyboard I have a UITableViewController-->UITableView-->UITableViewSecion--> with static cells
in the same UITableView I also have a UIView that holds a background image and UITextView.
A click on a button shows the UIView and set it's frame, which appears OK, but as soon as I click on the UITextView or make it firstResponder programmatically the keyboard appears and the view disappears
the code when clicking the button
self.myView.frame = CGRectMake(0, self.tableView.contentOffset.y+100, self.tableView.frame.size.width, self.tableView.frame.size.height-100);
self.myView.hidden = NO;
how can I fix this?
Can you copy paste us the code where you add the view to the tableview? Are you doing it with constraints or with frames?
The issue you are having is probably due to the fact the UITableViewControllers automatically shrink the contentSize of the UITableView they hold when the keyboard shows. If you add a UIView to your tableView with addSubview: programmatically, you might need to add a flexible bottom resize mask to make sure when the contentSize shrinks in height, your view stays attached to the top and not the bottom.
Try this on viewDidLoad:
[theViewYouAddedToTableView setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];

UILabel with horizontal padding?

I'm new to ios development and i'm struggling to make something that should be trivial.
I want simple container with background color and single line text inside with padding and i prefer to make it programmatically (no IB).
What i've already tried:
simple UILabel vertical padding is not a problem with fixed height but no horizontal;
also UIView with UILabel as subview - adding background color to UIView and the label is just text sizeToFit;
When i am using UIView with UILabel as subview i've set:
[self.myTestView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
but because i'am using NSLayoutConstraint when i set self.myTestView.translatesAutoresizingMaskIntoConstraints = NO; i don't have proper autoresizing.
If it helps i'am trying this into UICollectionViewCell?
Just for additional information i know this can be achieved with UIButton and using UIEdgeInsets, but i don't want to use UIButton for simple text container.
Does anybody have an idea or direction?
Thanks in advance!
Why you just didn't add constraint to your label inside uiview in IB, with padding what you need?!
http://i.stack.imgur.com/8qTGt.png

Changing TextView properties in IBAction method

I'm trying to have some text in a UITextView change color (and sometimes change text) after a button press:
I have loaded a text view in ViewController with a simple string in viewDidLoad
self.answer1TextView.text = questions[1];
The above displays just fine.
There is button next to it, which triggers the following method (also in ViewController):
- (IBAction)select1ButtonTapped:(id)sender {
self.answer1TextView.textColor = [UIColor redColor];
}
The problem is this: when the button is tapped, the text in the UITextView doesn't change color, and the right half of the view disappears.
I'd greatly appreciate some advice on this. Thank you.

How to generate a image of uiscrollview?

I have a UIScrollView with subview like UILable and webviews. So when I click on a button all the content of the scrollveiw should generate as a image. I tried to generate it but it is not getting all the content of UIScrollView it is just showing only visible view.
and my code is
[Scroll.layer renderInContext:UIGraphicsGetCurrentContext()];
It is because your scrollView's contentSize is larger but its scrollView.frame is still smaller.
Instead of scrollview, add your content first into a UIView (Let say it containerView). containerView should have height-width stretchable according to your content.
After that add your containerView to scrollView. So your scrollView require contentSize = containerView.frame.size
Now try to taking screenshot for the IBOutlet of containerView, not the scrollView.
I have not tried this out, but I guess it should work in this case.

UIScrollView + UIToolbar problem

I got a small problem with my UIScrollView. I've added the scrollview to my UIView with Interface Builder. I've also added a UIToolbar as a subView (programmatically if it matters) to the view.
Now, my problem is that the scrollview scrolls under the UIToolbar. It doesn't "stop" at the top of the toolbar as it should to. It works if I change the size of the UIView, but that can't be the right thing to do, because then the toolbar becomes non-clickable.
I hope somebody can get me on the right track, thanks! :)
Try doing something along these lines after creating your toolbar:
CGRect scrollFrame = myScrollView.frame;
scrollFrame.size.height = scrollFrame.size.height - myToolbar.frame.size.height;
myScrollView.frame = scrollFrame;