UITextView not scrolling to top when using textContainerInset - ios

I am using a UITextView to add an image and some attributedText to:
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.textView.editable = NO;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.textView setAttributedText:attrStr];
I then need to set the textContainerInset since I added the image as a subview (I want it to scroll with the text) so that the text doesn't go over the image.
self.textView.textContainerInset = UIEdgeInsetsMake(insetTop, 8, 8, 8);
But, for some reason, on iPad when loading it is scrolled to the bottom. So, I try to scroll to the top when loading using:
[self.textView scrollRangeToVisible:NSMakeRange(0, 0)];
But, that doesn't work. Removing the textContainterInset DOES allow it to scroll.
How can I still use the textContainerInset and scroll to the top? And should I be using a UIScrollView instead to add my subviews to?

Related

Adding sub view to UITableViewHeader getting wrong width?

First I build a view add to tableview as tableHeaderView
UIView *mainHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
[self.mainViewTableView setTableHeaderView:mainHeaderView];
I could adjust the Height of mainHeaderView but I cant adjust Y position of this headView.
And I trying to add a sub view:
self.searchForShop = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.mainViewTableView.tableHeaderView.frame.size.width, 44)];
[self.mainViewTableView addSubview:self.searchForShop];
Run app and found the subview's width is longer than tableHeadView.
Do view debug and found the subview is 16 width more than tableHeadView(I run in iphone5s).
How to fix it?
u can try like this, there might be a problem with setting the width of the tableview, just set the width of search bar to width of tableview
//create a tableview
tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate=self;
self.tableView.dataSource=self;
UISearchBar *searchBar=[[UISearchBar alloc]init];
/* option 1 ->[searchBar sizeToFit]; */
/* option 2 ->*/ searchBar.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44); //set the width how much table view has
searchBar.delegate=self;
[self.tableView setTableHeaderView:searchBar];

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

How do you add a UILabel to a UIScrollView?

I'm trying to add a UILabel to a UIScrollView. I followed the steps in this tutorial. But for some reason, the label is not showing on the scrollView when I run the app. The app consists of a storyboard with two files (ViewController.h and ViewController.m). In the storyboard there's only one view controller which is a custom class of ViewController. Below is my code which is in the ViewDidLoad method of ViewController.m.
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 250.0)];
testLabel.numberOfLines = 0;
[testLabel sizeToFit];
testLabel.text = #"test";
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 568.0)];
[scrollView addSubview:testLabel];
I tried the following lines of code at the end of my current, but still doesn't help.
[testLabel release];
[scrollView release];
You are calling [testLabel sizeToFit] before setting the text. This is setting the size of the label's frame to 0, 0.
Call sizeToFit after setting the text.
Make sure the view adds the scrollView as a subview too.
And a word to your fixes. Since Objective-C supports ARC (Automatic Reference Counting) the call "release" is not needed anymore.

ScrollView inside another scrollView

I have two UIScrollViews (red and yellow). Red is a subView of Yellow and grey is my device screen. I'm able make the scrolling works in both directions (vertical & horizontal), but there is one problem... Vertical scroll only works if the horizontal is SCROLLED FIRST?
Both of these views are NOT the firstResponder. I tried setting [yellow becomeFirstResponder] but it returned NO.
How do I make either UIScrollView scrollable upon user's touch?
you can do this by following code
UIScrollView *scrollYellow = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 40, 250, 480)];
[scrollYellow setBackgroundColor:[UIColor yellowColor]];
[scrollYellow setContentSize:CGSizeMake(250, 800)];
[self.view addSubview:scrollYellow];
UIScrollView *scrollRed = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 250, 200)];
[scrollRed setBackgroundColor:[UIColor redColor]];
[scrollRed setContentSize:CGSizeMake(500, 200)];
[scrollYellow addSubview:scrollRed];
hear in scrollYellow make width of frame & ContentSize same
and in scrollRed make height of frame & ContentSize same

ios uiview uiimageview uitextview

I have a main UIView that has an UIImageView on it, and the UIView adds an UITextView as a subview. UITextView's frame is smaller than UIView. UIImageView is actually a frame and the rest of the image is transparent and fits the full size of the UIView, but the UITextView is always upon UIView. What I want to achieve is UITextView shows UIView in foreground, while it is possible to scroll up and down on UITextView.
What is the proper way to make it possible?
My current code that does not work:
in loadView:
self.contentview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.contentview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ChristmasFrame.png"]];
self.contentview.opaque = NO;
self.contentview.userInteractionEnabled = YES;
[self setView:self.contentview];
in viewDidLoad:
[self.contentview addSubview:self.textview];
[self.contentview sendSubviewToBack: self.textview];
I want UITextView to be behind self.contentview, so it could show its background image upon the UITextView.
try setting setUserInteractionEnabled: to NO on UIView what is on top of UITextView
if that not working you could try to override hitTest:withEvent:
make clear color as textview's background color.
and add text view above UIView

Resources