I need to build a fairly detailed home screen for a IOS app and I'm fairly new to IOS dev. it basically contains -
Styled button which displays important message count and a link to messages area.
a iCarousel slider which shows styled stat info
a tableview with several dynamic custom classes.
All of which are wrapped with a UIScrollView - my issue is I dont know a method of working out the compiled height of the UIScrollview - Is it possible to give it an autoheight? If not how do I go about generating a height and applying it to the UIView?
Is it possible to give it an autoheight? If not how do I go about
generating a height and applying it to the UIView?
Yes, you can use it with Auto Layout. See the section "Pure Auto Layout Approach" at UIScrollView And Autolayout. UITableView is also a scroll view, so you will have to add a height constraint to it - the height can be calculated with sizeThatFits:.
Related
I'm trying to create a dynamic UIView that holds some fixed graphics elements and some dynamic text. I can't find a tutorial, or something in documentation that works for my project. Below is my exploded view (my view with scrollView and main view's height is forced to 800 in iphone 5 view, only for the pourpose of this question)
What do I want to acheive? The Header,the image,static view and static view 2 are all fixed, and they will not change with their contents. Label 1, Label 2 and the textarea will grow with their content size.
I have worked on this thing for about a day, and I need a little help because I can't achive any results. I have tried this solution:
solution 1
With this solution I can see in NSLog() the textview constraint that return a correct value
self.textViewHeightConstraint.constant = [textview sizeThatFits:CGSizeMake(textview.frame.size.width, CGFLOAT_MAX)].height;
but the textarea frame at runtime remains the same.
i have tried also this tutorial:
solution 2
It works for me in a project with only a scrollview, content view and textview. When i try to use that logic in my project it doesn't work anymore.
I have tried many other solutions, so my questions are: How I can achive the desired result? Or maybe there's something ridiculously wrong in my layout setting? Or simply, is there someone that can point my to a tutorial that cover a similar view organization?
You should avoid having a complex nested subviews inside a UIScrollView with AutoLayout, you should instead put them inside a subView. The best thing to start with:
1: Place a single UIView inside your UIScrollView with leading/trailing/top/bottom constraints set to 0 to the UIScrollView.
2: Place all the subviews inside the one single UIView,
3: Once you change the text/dynamic content of any of the subViews inside the UIView, you just calculate the scrollView.contentSize and set it accordingly by code needed for a set of complex nested subviews.
Your better option:
Work with a UITableView instead, it has the scrollView methods and it handles the dynamic layouts for you very easy without all the spaghetti code.
Looks like iOS makes our life difficult for simple things that should be accomplished in seconds with auto layout.
Anyway I want to place a UITableView inside a UIScrollView (I know that tableview contains scrollview but this is not what I want). The View ierarchy is
OuterView
ScrollView
ContainerView
View 1
TableView
The expected result is to disable tableview functionality and the scrollview to scroll view1 and tableview together.
The problem is that the height of the scrollview is the devices height whatever the device orientation. If i change the tableview frame and content size to the one calculated by all rows it does not adapt to the scrollview content size. Why? What's auto layout doing then?
I have set constaints like Trailing-Leading-Top-Bottom spacing to all view as well as width and height equality between outer and container view.
I cannot find a SIMPLE tutorial for scenarios like this one.
The Apple documentation discourages embedding UITableView within a UIScrollView because it can cause unexpected behaviour:
You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
This is likely why you are having trouble finding a tutorial. Why do you want to embed a table inside a UIScrollView? It may be more effective to designate rows in the UITableView to act as the content you wish to put in the UIScrollView.
I have been struggling past few days to implement a view that is scrollable using autolayout in Xcode 6.0 for Universal Apps. I have looked at many examples. However I am not able to get this working.
I am implementing this way:
View
--ScrollView
---ContentView (This view has all the subviews)
----- Labels
----- TextFields
----- Buttons
----- TableView
----- ImageeView
Setting the constraints of ScrollView to the View as zero for all the four edges. And constraints for the ContentView to ScrollView similarly all zeroes. Then I am adding constraints to each control setting the height and width. And the first control has the Center Horizontally and the last Control has the constraint to the Content View.
I can get the screen working if I don't add the scrollview. It is pretty confusing..
Unfortunately this does not resize on the iPhone. I am using .Xib for the Builder Interface. Please help me with an example or please guide me to any information that I can use to resolve my issue. Thanks in advance.
You can't create the scrollView contentView in the xib file.
You need to add then dynamically.
After you set the scrollView autolayout constraints in code you add all the objects:
1) set the contentView size (this could be as big as you want,don't need to set constraints to the contentView)
2) add the objects you want (labels, views, button...) to the contentView by calling
[scrollView addSubView:<your subView>];
I'm new to iOS development and am stucked with the problem to finish my layout with dynamic content..
Generally layout I'm trying to implement is quite popular, as an example:
and here is screenshot of my storyboard:
and table cell hierarchy:
The main question what are the main rules of building dynamic height table cell with dynamic height uiview inside it? The content could be long, so do I need to add constraints to bottom of the view?
Is it possible in Storyboard?
Thanks!
I don't think it is possible with storyboard (I might be wrong...): UITableView can have cells with a fixed size - using UITableView rowHeight property - or its delegate can provide a different size for any indexPath.
Constraints provided in Storyboard (or programmatically) only help layout the cell's subviews ONCE the cell's frame has been set.
So I think you should look to tableView:heightForRowAtIndexPath:, and a way to compute programmatically your cells' height dynamically.
I have a UIScrollView with several UITableViews embedded in it. I want to allow each table to expand its height as much as is needed to display all of its cells. The scroll view's frame takes up the whole screen, so its contentSize.height need to expand to fit the biggest table (which I think is the default behavior, but I mention it just in case I'm incorrect). Can this all be done on my storyboard? Or if I will need to add code to do it, I found this tutorial, but it's for iOS 6 - has any of the code for this constraint stuff changed for iOS 7?
If you know in advance how much room each table takes up you can do it purely in your storyboard, but because each UITableView is also a scrollview the default behavior of a UITableView is to fill the assigned size with content and scroll if there is overflow.
If you do not know the height of the tables at design time, you will need to set them at run time. See Autolayout a UIScrollView to fit content including subviews and grouped tables for an example, the short answer is to add a height constraint to your table, drag it into your controller as an outlet, and then set the height when you know it.
It can be done both ways.Manually moving UI elements through code(mentioning locations) and overriding it in did rotate or through mentioning constraints in auto layout.
This link http://www.doubleencore.com/2013/09/auto-layout-updates-in-ios-7/ provides details of ios7 auto layout .