I am programming an iPad application that uses auto layout to correctly position views on the screen in both portrait and landscape orientation. The layout (simplified) looks like this:
-UIView
-UIScrollView (all spacing pinned to superview)
-UIView (top, bottom spacing pinned to superview, leading and trailing spacing pinned to root view to prevent horizontal scrolling)
-UIImageView (leading and trailing spacing pinned to superview, height pinned to fixed value)
-UILabel (leading, trailing, and top spacing pinned to superview)
I have got the ScrollView to work, but I want the UILabel to scale vertically to fit its multiline content, which is not working. It does adjust its height based on the content, but it uses the (fixed) frame width specified in the storyboard to do so, instead of the actual width that is calculated by using the constraints, which changes when the device changes orientation. This leads to the UILabel being too high in landscape mode. Since a UILabel's content is vertically centered, this makes the top margin of the text too large.
I have no idea what is causing the trouble. Trying to add a height constraint to the UILabel and then calculating the height programatically using [NSString sizeWithFont:constrainedToSize], but this resulted in the exact same problem (I was unable to get the correct width as input). How do I fix this?
Although the reason for the problems is still unclear to me, I fixed the problem by subclassing UILabel and putting the following code:
#implementation MyLabel
- (void)layoutSubviews
{
self.preferredMaxLayoutWidth = CGRectGetWidth(self.bounds);
[super layoutSubviews];
}
#end
Related
I have a UITextView inside a UIScrollView. if I try to display a long text with a certain font size, the text is not showing. With shorter text or decreasing the font size the text shows up again. What am I doing wrong?
Screenshot of the autolayout here (the item selected is the textview):
The UITextView is inherited from UIScrollView
open class UITextView : UIScrollView, UITextInput, UIContentSizeCategoryAdjusting
So it is a bad practice to put ScrollView inside scrollView.
The reason why you don't see a long text in UITextView is that UITextView became scrollable. Also you didn't specify your scrollView content width so the with is calculated is as one line of your long text. But you can scroll it because you will scroll scrollView. Better way is to set label inside scrollview and set the label numberOfLines = 0. Make the label bottom padding to the bottom of your scrollView and top to some items in top of label and you will get a scrollable text.
I've just understood that you don't need a scrollView to place your long text.
You can use just a UITextView for your needs.
Remove your scrollView.
Add a UITextView.
Set constraints as described and math them with screenshot
Top Space to ReadingTimeLabel
Align Bottom to Superview / Bottom Layout Guide
Leading space to Superview
Trailing space to Superview
And you will get the result like on the screenshots
Scroller top:
Scrolled bottom:
Note: You can disable UITextView editing in attribute inspector.
But if you for some reason need a scroll view:
Set constrains as described, also match them as shown on screenshots.
Add scrollView constraints (Superview is the view where the scrollView is placed in):
-Top space to ReadingTimeLabel
-Align bottom to Superview/Bottom Layout Guide
-Align leading to Superview
-Align trailing to Superview
Please a UIView inside scrollView. Use it as container.
Add constraints for container. (Superview is scrollView)
-Align Leading to Superview
-Align Trailing to Superview
-Top Space to SuperView
-Bottom Space to SuperView
-Equal Width to SuperView
Add a UILabel inside container. (Superview is container).
Add constraints for UILabel.
-Align Leading to Superview
-Align Trailing to Superview
-Top Space to SuperView
-Bottom Space to SuperView
For the label set Lines = 0 in Attribute inspector or use the code
label.numberOfLines = 0
And you will get the result like on screenshots
Scrolled top
Scroller bottom
Try the below code:
textView.sizeToFit()
I have a view hierarchy on iOS arranged like this:
UIViewController
- UIView
- UIButton
- UIButton
- UIScrollView
- UIStackView
- UILabel
- UIDatePicker
- UILabel
- UIDatePicker
The scroll view is pinned to 20 from leading and trailing space on the superview, 0 from the bottom of one of the buttons, and 0 to the bottom layout guide.
The stack view has 0 leading, trailing, top, and bottom space to the superview (the scroll view).
I like how this looks in vertical orientation:
But in horizontal orientation, I think the long lines extending beyond the main content of the UIDatePicker are kinda ugly:
I've tried many permutations of constraints (max width + center X on the UIDatePickers, increasing various content hugging priorities, no width constraints on the UIScrollView and center X on it), but all have resulted in ambiguous layouts or conflicting constraints. How can I do what I'm aiming for here?
EDIT: With the constraints as suggested below (ScrollView pinned to container, StackView pinned with 0 Trailing, Leading, Bottom, and Top space to the ScrollView, and a width equal constraint on the date pickers) the StackView refuses to expand its width and stays pinned to the left of its parent.
This happens even though there are leading and trailing constraints set to 0 on the StackView.
Change the Alignment of UIStackView to Center.
Add a width constraint to DatePicker = 315
Now the stackview will keep the DatePicker to Center with width 315 in both potraite and landscape
I'm having this weird issue with my constraints which causes the UILabel (Caption Label) to be a fixed height instead of dynamically changing height depending on the text.
I have a view (Vertical View) with a top constraint on the label above it. The Vertical View contains a view (called View) which I'm using as a divider that is centered from top to bottom with a width of 1. On the left of the divider is a UIImageView (Left Image View) with constraints leading, top, bottom equal to superview and trailing equal to View. I want to do the exact same thing to the UIImageView on the right of the divider but here is where my issue comes up.
If I use a fixed height as seen below, the UILabel above Vertical View dynamically changes its height like I want but this is obviously not how I want the UIImageView on the right to appear. I want it to be similar to the UIImageView on the left of the divider with equal height and width.
If I set the top constraint of the UIImageView on the right to the superview Vertical View, similar to the UIImageView on the left of the divider, the UILabel above Vertical View doesn't dynamically change height anymore. The UILabel now has a fixed height which I believe comes from the fact that UILabel has a height of >= 14.
How can I properly set the constraints so that I can have both UIImageViews next to each other with equal and height contained within the Vertical View and still have the UILabel above Vertical View dynamically change height depending on the text that I set the UILabel to?
On the RightImageView, you first need to get rid of the "Height = 50" constraint. This is what is causing it to be small.
Next, if that alone doesn't fix you, can you try setting the following constraints instead of using the superview for the constrains (instead make it mirror the LeftImageView):
Left: Leading spacing to divider view
Top: Align top edges to LeftImageView
Right: Horizontal space to superview (your vertical container view)
Bottom: Align bottom edges to LeftImageView
This should allow the views to remain the same height and width (assuming your distances between left/right edge of vertical container view are the same, and the distances between divider are the same).
Now, ensure the size constraint for width of the divider is set to 1 and not >= 1. Also, ensure the vertical container view has a Compression lower than the Label.
One final note--your screenshot shows the result that IB is showing you (with the dotted yellow box) on the LeftImageView. One you update your constraints correctly, this yellow box should go away.
Regarding the UILabel - if you want this to grow dynamically, you need to do the following:
myUILabel.numberOfLines = 0;
myUILabel.text = #"Enter large amount of text here";
[myUILabel sizeToFit];
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 Controller with 4UILabels that are constant height throught all the iPhones, below the lastUILabel there is aUIView with a page controller(with aTableView inside that page controller).
What I want is that theUIView take all the height that he cant :
example: Screen of 600 height
4UILABELS = 200 height
TableView = it should get 400 height
Screen of 800 height
4UILABELS = 200 height
TableView = it should get 600 height
I need 1 constraint more to set the height of the view,What I have defined is :
EqualWidth constraints to superView
LeadingSpace to SuperView
Top Space to super VIew
The following constraints should align everything for you so that the UILabels stack vertically, each have height 50, and stretch to the edges. The UIView will take up the remaining space regardless of the screen size.
All UILabels
Pin leading edge to superview
Pin trailing edge to superview
Add height constraint set to 50.0
UILabel 1:
Pin top edge to superview top
UILabel 2:
Pin top edge to UILabel 1 bottom
UILabel 3:
Pin top edge to UILabel 2 bottom
UILabel 4:
Pin top edge to UILabel 3 bottom
UIView
Pin leading edge to superview
Pin trailing edge to superview
Pin top edge to UILabel 4 bottom
Pin bottom edge to superview
UITableView
Pin leading, trailing, top and bottom to its container view so it fills the area.
I assume that you are creating this UIViewController in a Storyboard.
You should be able to put all the labels in a container view which you give the constant height of 200 points, as you specified. Make a vertical constraint from "Top layout Guide" to said container view, maybe with the value 0. Drag an UITableView and place it below the container view. Dont give it any constraint regarding height. Make a vertical spacing between the (bottom of) container view and the (top of the) UITableView with value 0 and a vertical spacing between the (bottom of the) UITableView and the Bottom Layout Guide with value 0.
So it will be (V:0 means vertical spacing, constant 0):
Top Layout Guide
V:0
Container View - Height: 200
V:0
TableView (dynamic height)
V:0
Bottom Layout Guide
This should work.
(And then of course you need to create constraints regarding width and also internal constraints for each UILabel inside the container view. Let me know if you need help with that.)