UITextView will not resize - iphone

I have tried resizing a UITextView (inside a tableViewCell) to the content size. but it will not change its height at all. I have even changed the height of the UITableViewCell. What could be wrong?
- (void) setTextViewContents: (NSString*) string
{
[textView setText:string];
CGRect frame2 = self.frame;
frame2.size.height = 10000;
self.frame = frame2;
/* resize the view */
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height+60;
textView.frame = frame;
The string does appear on the view but the size does not change.

Try calling the -[UIView sizeToFit] method.

Related

Changing UIView size programmatically

I've a UIView, I want to change the size when user touches a button.
CGRect newFrame = self.myview.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
[self setFrame:newFrame];
CGRect newFrame = self.searchField.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
self.searchField.frame=newFrame;
None of them works, don't change anything. I want to set fixed width and height to UIView.
If I understand correctly, you want to change the size of self.myview. However at no point you are setting the frame of it. Instead you are trying to call sendFrame: on the view controller and some search field. I'm surprised, that the former one didn't give you a compiler error.
Objective C
CGRect newFrame = self.myview.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
[self.myview setFrame:newFrame];
Swift
var newFrame = myview.frame
newFrame.size.width = 200
newFrame.size.height = 200
myview.frame = newFrame
In my case, I had a constraint on the width of my view, so I couldn't change the width like Tim said.
What I've done : I created an outlet on my constraint, called myviewWidthConstraint for example. Then I used this outlet to change the width of my view like this :
mycell.myviewWidthConstraint.constant = newSize;
Size fields are read-only, just make a new one -
//Set height
let f = view.frame; //Grab current
view.frame = CGRect(x: f.origin.x, y: f.origin.y, width: f.width, height: 200);
view.frame = newframe;
or
view.center = newcenter;

Dynamic height of UIWebView with auto layout

I have a problem with setting the height of an UIWebView dynamically. The webview loads a product description which contains html. I want the UIWebView to change its height based on the contents of the description. The parent scrollview should also change its height, but then based on the height of the UIWebView.
Can anyone explain to me how I can achieve the desired behavior of my views?
This is my view hierarchy:
You have to add some code in the delegate method of UIWebView named webViewDidFinishLoad. Here is what you can do:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
yourScrollView.contentSize = webView.bounds.size;
}
The same thing can also be achieved using javascript to find the right height which is as follow:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect oldBounds = [[self webView] bounds];
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
NSLog(#"NEW HEIGHT %f", height);
[webView setBounds:CGRectMake(oldBounds.origin.x, oldBounds.origin.y, oldBounds.size.width, height)];
yourScrollView.contentSize = webView.bounds.size;
}
Hope it helps!
I actually had the same problem recently and ended up figuring it out and putting together a sample project on github.
You can set a constant height constraint on your WebView and change that dynamically once you calculate the height from the response.
Also constraint between a child view and a parent view can be achieved by this method in NSLayoutConstraint
+ (id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c

Dynamic UITextView in a dynamic UIScrollView - iOS

I'm facing a problem which takes me too long to solve.
I built a view, which contains a ScrollView. in the ScrollView, there's a view, which contains an image, and a UITextView. the textview should be in a dynamic height, with scrolling disabled. the textview gets all the text, but cut it off, and shows only the text that fits the height. in addition, the ScrollView doesn't changes.
- (void)viewDidLoad
....
//sets the text to the textview
[self.contentArticle setText:[NSString stringWithString:xmlParser.articleContent]];
//configures the scrollView
[self configureScrollView];
....
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect frame = self.contentView.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.scrollView.frame = frame;
[self.contentView sizeToFit];
[self.scrollView sizeToFit];
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
I did so many changes, and im not sure what is going on in there anymore!...
help would be sooo nice :)
UPDATE-
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
[self.scrollView setContentSize:textViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
}
Try
- (void)configureScrollView{
self.contentView.autoresizingMask = UIViewAutoresizingNone;
self.contentArticle.autoresizingMask = UIViewAutoresizingNone;
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.height = textViewFrame.origin.y+textViewFrame.size.height;
self.contentView.frame = contentViewFrame;
[self.scrollView setContentSize:contentViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}
Source code
had same issue , tried to find solution for many days and finally i got it working.
I have a textview inside a scrollview.
disabled autolayout from storyboards
ive added the textview to scrollview with addsubview
and finally set the content of the scrollview, to the textview frame height.
Below my code:
_textView.text = stripped; //(some string ,yours: contentArticle)
[_textView sizeToFit];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
_textView.frame = frame;
[self.scrollView addSubview:_textView];
[self.scrollView setContentSize:CGSizeMake(320,frame.size.height)];
Hope it helps!
You need to set self.contentView height first Use this code:
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
CGRect frame = self.contentArticle.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.contentArticle.frame = frame;
[self.scrollView addSubview:self.contentView];
frame = self.contentView.frame;
frame.size.height += self.contentArticle.frame.height;
self.contentView.frame = frame;
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}

Gap between subviews of UIScrollView

I have two subviews of UIScrollView. One is textView and other is tableView. I made textview's height flexible based on its content, but I have one problem. If the text in the textview (which is parsed data) is small, there is a big gap between textview and tableview. Or if the text is too large then it covers the tableview. How can I keep the same gap between textview and tableview irrespective of the amount of content of text view? This is all done in IB.
Thanks.
you must handle both textview and tableview position dynamically like ur text view height and y-axis is
y-axis 10; height 40;
now in tableview setframe:cgrectmake(your X coordinate, textview.frame.size.height+20)
so its always show same difference between textview and tableview if your textview height is 60 than tableview y point is 60+20
- (void)loadView
{
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self imageCount],
pagingScrollViewFrame.size.height);
pagingScrollView.delegate = self;
self.view = pagingScrollView;
}
- (CGRect)frameForPagingScrollView {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGRect pageFrame = pagingScrollViewFrame;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (pagingScrollViewFrame.size.width * index) + PADDING;
return pageFrame;
}

Custom UINavigationBar with custom height causes the UIBarButtonItem's to be positioned wrong

I created my own subclass of UINavigationBar in order to enable custom background that is taller than 44pxs.
I did it by overriding these two methods:
-(void) drawRect:(CGRect)rect
{
[self.backgroundImage drawInRect:CGRectMake(0, 0, self.backgroundImage.size.width, self.backgroundImage.size.height)];
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height);
return newSize;
}
And this is the result:
Now, my problem as you can see is that all the UIBarButtonItem's (and the titleView) are placed at the bottom of the navigation bar.
I would like them to be pinned to the top of the bar (with some padding of course).
What to I need to override to achieve that?
Thanks!
EDIT:
This is the solution that I used:
-(void) layoutSubviews
{
[super layoutSubviews];
for (UIView *view in self.subviews)
{
CGRect frame = view.frame;
frame.origin.y = 5;
view.frame = frame;
}
}
Does the trick for idle state, still have some weird behavior on push and pop items.
Try to override layoutSubviews: call [super layoutSubviews] inside and then reposition the items.
To solve the push/pop issue use setTitleVerticalPositionAdjustment in sizeThatFits:(CGSize)size
- (CGSize)sizeThatFits:(CGSize)size {
UIImage *img = [UIImage imageNamed:#"navigation_background.png"];
[self setTitleVerticalPositionAdjustment:-7 forBarMetrics:UIBarMetricsDefault];
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize newSize = CGSizeMake(frame.size.width , img.size.height);
return newSize;
}