Here is my structure of views for this
-ViewController
-UIView
-UIScrollView
-UIView * customview
Data comes form web Serivce. automatically increase the UIScrollView Height and subview Height.
I am fixing autolayouts to UIScrollView and UIView.
In customview UILables and UITableView.
You need to take the Outlet of your customeview height constrains.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
bind viewHeight with your customeview height constrains and set the constant property of your outlet
self.viewHeight.constant = 2000; //Your subview Height
in define image you can see the your subview height constrains and bind outlet with this constrains after it height will change dynamically as per your .constant value.
Related
I have a UILabel for item description amongst other views, all laid out using constraints in Interface Builder - you can see all relevant constraints in the image below. The number of lines is also set to 0.
I haven't set the height constraint because I want the UILabel to resize based on the text it contains. Instead, what happens is right after
[self.view layoutIfNeeded];
is called, the height of the UILabel gets set to 0. Even if I don't set other text to the UILabel, it has a default value of Item description set in Interface Builder.
The item title label above is set the same way, but that one doesn't get squashed to 0, so I'm a bit confused.
Anyone had any experience with such behaviour?
I managed to solve it by setting the UILabel's vertical compression resistance priority to 1000 (default 750) in Interface Builder.
Since my views are embedded in another view, and the parent view's bottom is dependent on the bottom of the lowest child view, I only speculate that the UILabel without a height constraint was getting squeezed in the process of laying out the views. Probably playing with priorities of other constraints somewhere down the chain would have yielded the same result, but I wasn't able to do it successfully. The solution above, however, worked, which is good enough in my case.
Hope this helps someone.
Set 3 constraint
1.Leading space to superview
2.Trailing space to superview
3.Top space to superview
then
#property (nonatomic, strong) IBOutlet UILabel *lbl;
- (void) viewDidLoad{
[self.lbl sizeToFit];
}
ctrl drag from the label to itself > select height > set the constant of the height to 0 and change equal (==) to greater than or equeal (>=)
I think you need to set 5 constraints on your label :
Leading space to superview
Trailing space to superview
Vertical space to "Item"
Vertical space to "Name"
Height
Then add an IBOutlet in your controller on the constraint height (let's say labelHeight).
So in your viewDidLoad you will be able to set this constraint value:
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelHeight;
- (void) viewDidLoad{
[self.label sizeToFit];
labelHeight.constant = self.label.frame.size.height;
}
AutoLayout in this UIViewController can't satisfy all the constraints you have set, therefore it dismiss those on your UILabel, resulting in a compressed state. You should have a look at the other constraints in your UIViewController, and set the priority of the height contraint to a higher number.
I have a UIView created from a xib file. Inside that UIView there is a UIButton with a text that may be longer or shorter depending on the language.
What I want is to resize the parent UIView to fit the width of the UIButton. How can I do it with AutoLayout, or programmatically?
Thanks,
add leading, trailing, top and bottom constraint of button
add width constraint of view and connect to viewWidthConstraint.
calculate width of button for language and set constant of viewWidthConstraint.
View and button will resize
#property (nonatomic, weak) IBOutlet NSLayoutConstraints *viewWidthConstraint;
- (void)viewDidLoad {
super.viewDidLoad();
self.viewWidthConstraint.constant = [self buttonWidth];
}
- (CGFloat)buttonWidth {
return 100.f; (calculate width of button)
}
I have UIView with auto layout constraint. My view contains subview. I have height constraint that I attached to my view controller as a property.
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewHeightLayoutConstraint;
In view did load I set:
[[self headerViewHeightLayoutConstraint] setConstant:0.0f];
But I still see subviews. So I check Clip subviews in storyboard for that view but I still see subview.
I am using autolayout. In xib i have a scrollview as a parent view. Added one view as a subview. For that subview i have added four child elements. Three are views(say view1,view2,view3) and one is label which is having multiline text.
I am using the below code to change the height of the view
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *view1Height;
self.view1Height.constant = 200;
In this case there is no issues with content size the view2 and view3 frames are adjusted automatically.
But while coming to multiline label I am using the below code for setting label text
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *textLabelHeight;
self.textLabelHeight.constant = expectedLabelSize.height;
But here I am facing issue with scroll content size. Unable to view the entire text. Similarly I want to place one more view below the label how to set constraints for it.
I want to change position of UITableview position programmatically. I have added following code in viewDidLoad,ViewWillAppear callbacks, but position is not changing.
Table.frame = CGRectMake(0,100,320,200);
When I disable Auto Layout option in Storyboard, Table View's position is changing. But, I need to keep Auto Layout option to maintain support for different device type.
Use autolayout, and set constraints to your tableView's position & size.
Then, create IBOutlet instances of the type NSLayoutConstraint, and attach them to your constraints in the xib.
Then, in your code, set the values of the constraints to reset the table view position.
Sample code:
in your .h file:
#property IBOutlet NSLayoutConstraint *tableOriginX;
#property IBOutlet NSLayoutConstraint *tableOriginY;
#property IBOutlet NSLayoutConstraint *tableWidth;
#property IBOutlet NSLayoutConstraint *tableHeight;
Then, in your .m file where you want to alter your table's frame:
tableOriginX.constant = 0;
tableOriginY.constant = 100;
tableWidth.constant = 320;
tableHeight.constant = 200;