I am pretty new to iOS dev. I am trying to create a view with a UILabel and a UITableView. The UILabel is used to display some header text & the tableview is positioned below the header & it covers the remaining portion of the screen.
I am creating this view using code & not the storyboard approach. So I need to specify the height & width of my tableview as constraints. For the width I have set a constraint where it matches the width of the container view. However for the height I wish to use the following equation:
tableview's height = container view's height - UILabel's height
For this I need to know how I can get the container's & the UILabel's height via code. I tried the following approach-
CGFloat header_height=lblHeader.bounds.size.height;
But this gives the same height as the container view's height .i.e. 460. Can someone please explain how I can get the height of the UIlabel view?
To get the height of the UILabel do this instead:
CGFloat header_height = lblHeader.frame.size.height;
Frames and bounds are not the same.
Cocoa: What's the difference between the frame and the bounds?
Related
I have created a UIView that consists of 3 UILabels and 1 UIImageView as below -
The UILabels have lines set to 0 so that the height of the UILabels change dynamically
I have added constraints such that the height of the image depends on the height of the UILabels as shown below -
I have set width of the UIImageView to a constant of 100.
I would like to set a constraint on UIImageView such that the height of the UIImageView is equal to the height of the whole view.
I tried adding a "Equal Heights" constraint on the UIImageView and the whole view. I observed that the height of the whole view depends on the height of the UIImageView and not that the height of the UIImageView depends on the whole view as I want it to be.
Can anyone point out how I can add a constraint to the UIImageView such that its height is equal to its superview but the height of the superview is not dependent on the UIImageView?
Edit -
The below image shows the problem that I am facing. When an image is set to the UIImageView, the height of the view changes.
I want the height of the UIImageView to be equal to the height of the view, but I do not want the height of the view to be dependent on height of the UIImageView
Looks like you need to reduce the vertical content compression resistance on the image view.
Set it to something very low like 100 and try again.
I believe you haven't add the height constraint to the superview. Setup height constraint for the superview and then add a "Equal Heights" constraint on the UIImageView and the whole view.
In fact default compression resistance of your UIImageView is 750, while content hugging of root UIView is 250. This means AutoLayout engine layouts everything correctly. You can change vertical compression resistance to value lower then 250 (UILayoutPriority.defaultLow - 1) to achieve desired layout.
I am trying to arrange 3 views - 1. Image view with fixed height and width 2. Textview with fixed width but dynamic height and 3. Tableview with fixed width and height adjusted according to available space after textview.
What constraints I am missing here. Why I need to give Textview or Tableviews Y pos or height constraint. Doesn't it make their height fixed?
You have to put a height constant to your textview. Because autolayout can't figure out how many pixels to give to your both view.
If you want to adapt your height depending on the text, you can use :
TextViewHeightConstraint.constant = [TextView intrinsicContentSize].height
into your code after your link your constraint via iboutlet.
In UITableview I am adding another UIView, it's height should be dynamic according to cell height, the problem I am facing is it is not accurate.
The height of UIView is not varying correctly with cell and it is calculated again and again on table reloading.
When you load your data, you can calculate the each cell's height, and store them in a array, then you can use this frame array to setup the uiview's frame
You have to provide constraints on your UIView according to contentView of cell top,bottom,leading,trailing value 0 , So your UIView height and width is same as cell height & width.
Please refer http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1 for learning autoLayout for beginner.
I have been searching through stackOverflow and whatever google proposes but I wasn't able to get it to work. I am intending to draw a simple 2D Graph in a scrollview, the distance between my datapoints is kStepX and I want the scrollview to be at least the width of the screen, if I have more datapoints it should scroll but no more than 100 points.
I think I have a problem with my Autolayout and sizing the contentWidth, so here is what I have done so far:
I added a UIScrollView with the following constraints:
Leading Space to Superview =0
Top space to superview =0
Height = width of superview
Width = width of superview
I then added a UIView (called GraphView) as a Child with the following constraints:
zero space to all 4 bounds of scrollview
center X and center Y to scrollview
in my GraphViewController I set the contenSize as:
historyScrollView.contentSize = CGSizeMake(MAX(SCREEN_WIDTH,sizeof(data)*kStepX), kGraphHeight);
but it does not scroll!
if I set a fix width in the storyboard the scrollview scrolls further than I have data..
What am I doing wrong?
You should not be setting the contentSize of the scrollView when using auto layout. That should be calculated from your constraints if they are setup correctly.
What you are missing is to set a width and height constraints on the view inside the scrollView. A scrollView determines it's contentSize based on the size the subviews have. Since an UIView does not have an intrinsic size, you will need to add width and height constraints to it, then update it when you need it with the right value
Something like this should work:
innerViewWidthConstraint.constant = MAX(SCREEN_WIDTH,sizeof(data)*kStepX)
innerViewHeightConstraint.constant = kGraphHeight
// You might need to layout the views too
[historyScrollView layoutIfNeeded]
Hope this helps! Good luck :)
Take ScrollView in side in your Main View and give Top,Bottom,Leading,Trailing,Center X and Center Y.
after take GraphView inside you scrollview and also set constrain of GraphView.
set height of GraphView
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *HeightGraphView;
whenEver you set your HeightGraphView then automatically set ScrollView contentSize.
see my demo its working for Vertically scrolling, apply same for Horizontal scrolling.
Demo Vertical Scrolling
I have a simple case:
I have the following views hierarchy:
View
ScrollView
View
Label
My label is positioned at the bottom of it's super view, and it's height is changed dynamically, depending ot the size of the text that it is rendering.
My aim is to adjust the label size depending on the text, so that no text is truncated, and with the growth of the lines of the label, to grow the scrollview's content size, so that the label always is positioned at the bottom.
How can I do that with autolayouts, preferably from IB only?
If you want the UILabel to stick at the bottom of its parent UIView using autolayout, then that view won't expand its height when the UILabel height increase, what really happens is that the UILabel will move up to occupy more area.
I f you wish your UIView to expand, then don't use autolayout in that UIView, and position your UILabel at a constant origin, then change the view's height & the scrollview height as per the UILabel text.
you can get the UILabel size using the below line of code:
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];