I currently have a UIScrollView with multiple UIView inside. And inside each UIView I want to put a UIWebView. The height of each UIWebView is set dynamically depending on the content. How could I do now to set the height of each UIView depending on the size of UIWebView ?
Thanks
Try something along the lines of:
uiView.frame = CGRectMake(desiredX, desiredY, desiredWidth, uiWebView.frame.size.height)
You should use auto-layout and align the bottom of the UIWebView with the UIView.
[uiView alignBottomEdgeWithView:uiWebView predicate:#"10"];
Example: http://code.dblock.org/ios-uiview-with-an-image-and-text-with-dynamic-height
Related
I am having more lines of contents which exceeds the ViewController height, I want to show it in same screen by scrolling. Is there any way to achieve that?
Use a UIScrollView and put your contents in it.
You can use UIScrollView or UITableView for the same.
Add all views as subviews of one super view say containerView. Add containerView as sub-view of UIScrollView. Do not forget set appropriate content size of UIScrollViiew. You can do it by proper autolayout or programatically as
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
You can also use UITableViewController with static cells. You can add view in static cells and you are good to go. UITableViewController itself manages scrolling.
Hope it helps. Happy Coding!!
You can use a UIScrollView (As #luckyShubhra and #chedabo suggested) , you can setup the UI correctly in Storyboard in this case and have the contentsize of the UIScrollView set to the size of the viewController contained in the UIScrollView...
Then also the UITableView (As #luckyShubhra suggested)... if your data is setup to deal with as a list of NSStrings or object, this can work well...
Then if the content is a NSString, and you would use a UILabel, I would suggest changing it to a UITextView... This will make it possible to scroll as well. If the contents is not to be edited just change the Selectable and Editable booleans to false...
UIScrollView or even UITextView can help that too. if it is HTML, you may consider of rendering it with UIWebView.
just make sure the control height is lesser than the view controller, then it will render the contents in a scrollable manner.
I have an UIView inside which i have a UIButton. I am using auto layout. Now I want to reduce the height of the UIView. How should I do it ? On reducing the height of the UIView , the UIButton inside the view should also move up at the same time. How can i do this using Auto Layout ?
You can create IBOutlet for height constraint of you view and you can change value for it.Just check how to create.
Now use the code below to set height of you UIVIew
heightConstraintOfView.constant = DYNAMIC_HEIGHT;
I have a UIView that contains two subviews - a UIImageView or a UITextView. The UIImageView has a fixed height and width.
The UITextView has variable size.
Only one of the UIImageView and UITextView would be displayed at a time. I plan to accomplish this programmatically by setting hidden = YES.
I would like the UIView to hug whichever child view is not hidden with no margin.
I would like to accomplish this with IB and autolayout if possible because the rest of the view is built this way.
So far I have created constraints that link the 4 edges of both of the subviews to the parent view and constraints for the height and width of the UIImageView. Naturally this creates a content priority ambiguity.
I would appreciate any advice.
Thank you!
So the contain view has to have trailing constraint and constraint with bottom which should have IBOutlet(s) references in viewcontroller and then when the text changes :
- (void)textViewDidChange:(UITextView *)textView {
[self.textView sizeToFit];
self.containerViewTrailingConstraint.constant = self.textView.contentSize.width;
self.containerViewConstraintWithBottom.constant = self.textView.contentSize.height;
[self.view layoutIfNeeded];
}
And when the textView is hidden then you have to set the self.containerViewTrailingConstraint.constant, self.containerViewConstraintWithBottom.constant in relation with the imageView
I subclassed UIView: http://pastebin.com/MVk1XmPS and when I add it to another view of a UIViewController:
self.myView = MyView.newAutoLayoutView()
self.myView.backgroundColor = UIColor.redColor()
self.view.addSubview(self.myView)
the red background is missing. It seems that myView has no width and height. However the content is visible:
I have to hard code the dimension:
self.myView.autoSetDimensionsToSize(CGSize(width: 100, height: 100))
but shouldn't it grow automatically by the size of the content? I use swift, xcode6.1 and iOS8.1
Views do not automatically grow to fit their content. If you enable clipsToBounds on your view, you will see the content disappear. Alternatively, if you put a button as part of your content, you will see it but be unable to interact with it because it is outside of its superview's bounds.
In your subclass, you could implement the sizeToFit or sizeThatFits: methods, but these are not going to be automatically called by the system. You should consider assigning constraints for your view in your storyboard. If you're not using a storyboard, you will need to assign a frame in your viewWillLayoutSubviews or layoutSubviews method.
Storyboards will also like your custom view better if you implement intrinsicContentSize.
I only want to activate vertical but not horizontal scrolling. How can I achieve this in the UIScrollView class?
Make use of contentSize property of the UIScrollView.
For example, if your scrollView size is 260 pixels wide,then
CGSize scrollArea = CGSizeMake(260, scrollableHeight); //you can use yourScrollView.width instead of 260 if you assigned its frame already.
[yourScrollView setContentSize: scrollArea];