I have a UIView in top of UITableView (drag and drop in storyboard . I tried to hide and show that view. but table view stands in place . how can i change frame of uivew so when it is invisible tableview goes all the way to top?
PS. I will provide more info for those who are willing to help. Thanks in Advance
http://i.stack.imgur.com/cM3tA.png
![the view with hamburger button and textfield)
With autolayout:
Add height constraint to your UIView with button and textField
Create Referencing Outlet for this height constraint (lets count it will be "searchViewHeightConstraint")
Add Vertical Spacing Constraint between the UIView and the UITableView
In code - when you need to hide UIView - just set its height constraint to 0
self.searchViewHeightConstraint.constant = 0.f;
Also you need to store somewhere the initial height of the UIView (when it is visible) and set its Height Constraint to this value when you need to show UIView.
Without autolayout:
Save the value of UIView frame height
Set the height of UIView to 0
Decrease the y-coordinate of UITableView frame with saved height of UIView
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y -= viewInitialHeight;
tableView.frame = tableViewFrame;
Related
I am facing a trouble with UIScrollView using auto layouts. I have the following view hierarchy with constraints.
ViewA (leading,trailing,top and bottom spaces to superview).
-- ViewB (leading,trailing,top and bottom spaces to superview).
--- UIScrollView (leading,trailing,top and bottom spaces to superview).
Here ViewB is adding on ViewA and i have some textfields,buttons and UITableview these all are placed inside UIScrollView.This is my view hierarchy.
Now i want to my Scrollview height increase dynamically based on tableview number of rows.
For this I'm using bellow code but it not working.Can any one please suggest me.
CGFloat height = MIN(self.view.bounds.size.height, placeTbl.contentSize.height);
self.Height_PlaceTbl.constant = height;
[self.view layoutIfNeeded];
if you want to increase your ScrollView height based on tableview number of rows you can try this
first get number of your tableview rows and then get height and width of your scrollview
let width = self.scrollview.layer.bounds.width
let height = self.scrollview.layer.bounds.height
after this , you can increase height of your scrollview like this
self.scrollview.frame = CGRectMake(0, 0, width, height + numberofrows)
I am creating a chat interface and have a UITextview and button for sending message inside a UIView. The height of UITextView changes based on its content size but UIView height does not change with it. Chat view
I will appreciate any help on this.
Here are constraints on the message field Message field constraints
If you programmatically created the view:
containerView.frame.size.height = textView.frame.size.height
If you create the view in the storyboard
Create a height constraint for your view. Then connect that constraint to your code. Then run this:
heightConstraint.constant = textView.frame.size.height
Set autolayout constraints specifying a fixed space and pin them from your UIView top to the UITextView top and from your UIView bottom to the UITextView bottom.
It is unclear from your question how you are setting the height of your container view and your text view. You should have a height constraint on the text view and change its constant when you want to resize the text view. Do not change the frame directly. The container view must not have a height constraint - it should resize solely because of the bottom and top constraints to the text view.
without auto layout code:
txtView.frame = CGRectMake(view.frame.origin.x + 8, view.frame.origin.y + 8, view.frame.size.width - 100, view.frame.size.height - 16);
btnSend.frame = CGRectMake(view.frame.size.width - 92, (view.frame.size.height/2) - 25, 84, 50);
I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout
I have a view (let's call it "contentView") that its height is determined by its subviews, which is mostly UILabels.
The UILabels content is dynamic and may grow or shrink at runtime.
The UILabels have numberOfLines = 0, and no height constraint, which allow their height to grow with the content.
The UILabels have 0 leading/trailing/top/bottom constraints to their neighbors and/or superview, as follows:
The result is having contentView's height equal the total height of all its sub UILabels (with their height being determined by the height of their text).
I now need to hide contentView on a press of a button (and show it again when the button is pressed again).
Also note, that while animating, if there are other views below "contentView" they need to move in to take the empty space (and move back out when the button is pressed again).
It seems the most natural animation is to change the height of contentView to 0, while animating the views under contentView up at the same time (with the reverse animation changing the height back to its original height).
Any suggestions on how to animate contentView's height?
Unfortunately I was not able to create a smooth enough animation for this layout, but I found a workaround.
Constraints change the layout while animating, and I needed the opposite effect - the layout had to remain fixed while the animation is in progress.
So I disabled the constraints for the duration of the animation, and that seemed to work well.
The full recipe is:
I disabled the constraints on contentView:
contentView.translatesAutoresizingMaskIntoConstraints = YES
I then did a simple frame animation. I made sure to calle layoutIfNeeded on superview to make sure the view that's under contentView moves up during the animation:
CGRect frame = view.frame;
frame.size.height = 0;
[[view superview] layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
view.frame = frame;
[[view superview] layoutIfNeeded];
}];
I also had to change some properties for the subviews to have them animate correctly (this step might not be needed. It depends to the base layout of the subviews):
for UITextView: scrollEnabled = YES
for UIButton: clipsToBounds = YES
At the end of the animation, on the completion block, I set contentView.translatesAutoresizingMaskIntoConstraints back to NO and restored the subviews properties:
for UITextView: scrollEnabled = NO
for UIButton: clipsToBounds = NO
(I ended up using UITextViews instead of UILabels because I needed the text to be selectable. I then set scrollEnabled to NO to size the textviews based on their content).
Yes: add a height constraint with a constant of 0.
You can use deactivateConstraints() to temporarily disable other constraints that might conflict with this, and activateConstraints() to re-enable them.
You could put the relevant constraints in an array programmatically, or use an outlet collection if you're using a storyboard.
I have placed UIView in UIScrollView in Storyboard. The UIView is pinned to the all the sides of the UIScrollView. I am trying to set the size of UIView programatically because it is needed for scrolling to work properly. How can I programmatically set the width and height of the UIView?
Thanks for any help as I am trying to solve the problem already for a week...
You'll need to change the frame of your view as below:
yourView.frame = CGRectMake(x,y,calculatedWidth,calculatedHeight);
Plus, you'll also need to check for the scroll view's frame so that it enlarges as per your view's width and height.
You need to set new frame to change width, height, x or y for example:
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight);