Is there any way to change the height of table view according the size of array?I am using table view as a drop down so i need to change the height of table view according the number of elements in array.
First get the row height from the storyboard and create outlet of the tableView height
then use the code as follow
as if row height is 50
(Swift code )
let arrayCount = yourArray.count
let tableHeight = arrayCount * 50
tableHeightConstraint.constant = tableHeight
this will calculate the count of your tableView array and assign the same height to your tableView
Calculate the height of each row,and then
height = rowHeight*array.count;
tableview.frame = CGRectMake(x,y,width,height);
if you use autolayout,add a constrant like
[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];
Related
I have two button with equal width constraints. I want to remove equal width constraint and add a new width constraint for one button. the other button constraint to zero.
This is what I've tried. But it's not working. The Equal width constraint is not removing
NSLayoutConstraint * constraint = [self
constraintWithIndientifer:#"MyButtonWidth" InView:self.view];
[self.view removeConstraint:constraint];
NSLayoutConstraint * newconstraint = [NSLayoutConstraint constraintWithItem:self.departureButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:self.view.frame.size.width];
newconstraint.identifier = #"MyButtonWidth";
[self.departureButton addConstraint:newconstraint];
[self.view layoutIfNeeded];
-(NSLayoutConstraint *)constraintWithIndientifer:(NSString *)identifer InView:(UIView *)view{
NSLayoutConstraint * constraintToFind = nil;
for (NSLayoutConstraint * constraint in view.constraints ) {
if([constraint.identifier isEqualToString:identifer]){
constraintToFind = constraint;
break;
}
}
return constraintToFind;
}
You don't need to do this,
you can add another constraint such as width constraint to >=1
after that, you should set "equal constraint" "priority to 1
after that, you should set "width constraint" priority to 2 or more(default is 1000)
after that, when you set constraint constant it works and equal constraint is not working because of priority:
self.yourButtonWidthConstraint.constant = yourWidth;
If you look at the documentation for NSLayoutConstraint, you'll find an isActive property:
You can activate or deactivate a constraint by changing this property. Note that only active constraints affect the calculated layout.
Activating or deactivating the constraint calls addConstraint: and removeConstraint: on the view that is the closest common ancestor of the items managed by this constraint. Use this property instead of calling addConstraint: or removeConstraint: directly.
So going by that I'm changing both the removeConstraint and addConstraint you have in your sample code.
aka just change your code to this:
NSLayoutConstraint * constraint = [self
constraintWithIndientifer:#"MyButtonWidth" InView:self.view];
constraint.active = NO; // CHANGE 1
NSLayoutConstraint * newconstraint = [NSLayoutConstraint constraintWithItem:self.departureButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:self.view.frame.size.width];
newconstraint.identifier = #"MyButtonWidth";
newConstraint.active = YES; // CHANGE 2
[self.view layoutIfNeeded];
NOTE: the NSLayoutConstraint class also has two class funcs: + activateConstraints: and + deactivateConstraints:, which both take an array of constraints. That's preferred when you're changing multiple constraints, but not needed here since its only 1.
Loop though the constraints of the parent view of these two buttons and delete the constraint where first item is the first button and second is the second button and layoutAttribute is width
for (NSLayoutConstraint*co in self.parentOfBtns.constraints)
{
if(co.firstItem==self.btn1&&co.secondItem==self.btn2&&co.firstAttribute==NSLayoutAttributeWidth&co.secondAttribute==NSLayoutAttributeWidth)
[self.parentOfBtns removeConstraint:co];
}
then add your new width constraint , and don't forget to call
[self.view layoutIfNeeded];
I have two views, a UIWebView and a UITableView, that I am programmatically expanding their heights. Both of these views are located inside a UIScrollView. Both the WebView and TableView have had their scrolling disabled, and the ScrollView and its parent views have Autoresize Subviews turned off. What I'm trying to do is expand both item's heights to the appropriate sizes, then expand the ScrollView's content size to the sum of both items. Right now that works. However, as soon as I scroll the TableView and WebView reset to their original heights, but the ScrollView maintains its new content size.
I am changing the heights in webViewDidFinishLoad with the following code.
// Set the webview height //
webView.frame = CGRectMake(
webView.frame.origin.x
, webView.frame.origin.y
, webView.frame.size.width
, webView.scrollView.contentSize.height);
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
[webView addConstraint:[NSLayoutConstraint
constraintWithItem:webView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:webView.scrollView.contentSize.height]];
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
// Set the table height and location //
self.myTableView.frame = CGRectMake(self.myTableView.frame.origin.x
, webView.frame.origin.y + webView.scrollView.contentSize.height + 8
, webView.frame.size.width
, [tableRows count] * 40);
[self.myTableView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.myTableView addConstraint:[NSLayoutConstraint
constraintWithItem:self.myTableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:[tableRows count] * 40]];
// Update the scrollview //
CGSize newSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, webView.frame.size.height + self.myTableView.frame.size.height);
[super updateViewConstraints];
[self.myScrollView setContentSize:newSize];
I'm guessing I'm missing a step somewhere.
I managed to figure out what was wrong. setTranslatesAutoresizingMaskIntoConstraints need to be set to YES when inside a ScrollView. This is NO by default so removing that line did not work, it must be explicitly set to YES
The problem you're having is that you are trying to use both static frames and constraints at the same time. This would not work and is the reason why the frames of the tableView and the webView go back to their initial values.
Making this work with constraints will be a little bit more complicated, but if you just use the static frames and remove the constraints you're adding by code, all should work fine.
Good luck!
In my app I need to have a UITableView in half of one of the views (from half view to the bottom). The problem is when I rotate the device, the tableview changes its position to always respect the height constraint.
UITableView at portrait
UITableView at landscape (when the problem occurs)
The main idea is to shrink the tableview to always fit half of the view.
I assume you are setting the frame of your tableView in viewDidLoad or init; instead, use viewDidLayoutSubviews in your view controller to layout your views. This is called every time the view controller's bounds changes, even after a rotate:
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.tableView.frame = CGRectMake(0, CGRectGetMidY(self.view.bounds), self.view.bounds.size.width, self.bounds.size.height / 2);
}
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.tableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
I have a View controller, in which the view has two image views and two text views. I turned off auto layout, and I programmatically set the distance between the first text view and the first image view by using this code:
The following code is in the viewDidLoad method of my custom view controller class. I have set the autoresizing mask to no in both cases, so I have no idea why the code doesn't work.
(tf2_logo is the image view and itemName is the text view)
self.tf2_logo.translatesAutoresizingMaskIntoConstraints = NO;
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.itemName attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tf2_logo attribute:NSLayoutAttributeTop multiplier:1.0 constant:-1.0]];
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.tf2_logo attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backpackBackground attribute:NSLayoutAttributeLeft multiplier:1.0 constant:17]];
Now I want to do the same thing with my other text view, basically I wanted to keep the distance between the itemName text view and the text view at a certain distance. I used this code:
(tf2 is my other text view)
self.tf2.translatesAutoresizingMaskIntoConstraints = NO;
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.itemName attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tf2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:-3.0]];
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.tf2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tf2_logo attribute:NSLayoutAttributeRight multiplier:1.0 constant:20]];
After implementing this code, the tf2 text view doesn't even show up in the view controller. What is the problem?
EDIT: You can download the whole project here: https://www.dropbox.com/sh/u820u2ndyrncuz8/P4atI-9CAx
EDIT#2:
You mentioned that you turned off auto layout, because UITextView has that little gap on top in iOS7. To remove the gap, try this:
self.tf1.textContainerInset = UIEdgeInsetsZero;
When you log the original value of the textContainerInset it shows: {8, 0, 8, 0} . The two 8's are responsible for the gap (one at the top). The line above sets all values to zero and the content is nicely aligned to the top of the frame.
(EDIT#1: Completely changed the answer)
I assume you primarily want to have a flexible height of the imageName UITextView. First I suggest to use auto layout. You can set constraints in Xcode according to the following image:
The red lines are the constraints. The green line is special: It shall be a height constraint and you create an outlet for it in the view controller. (Open the document outline view, locate the height constraint in the tree and control-drag it to the code.)
Then in the viewDidLoad method:
CGSize size = [self.tf1 sizeThatFits:self.tf1.frame.size];
self.tf1Height.constant = size.height;
The height of the "lore ipsum" field now adjusts to its content.
Have you tried using frames instead of constraints? If your not using autolayout I think frames might be easier to read/implement.
sample:
// tf2 will be placed at (0,0) in superview and have width of 100 and height of 20
tf2.frame = CGRectMake(0, 0, 100, 20);
you can play around with different values to get your layout as desired.
I have a custom cell with a subview inside it which on getting content from server increases in height once the response comes.
So I increase the height of this subview with animation, and on completion of the animation I send a delegate message to viewController.
in this delegate I set a instance variable with new value for the cell height and call
[tableView beginUpdates];
[tableView endUpdates];
after that which just resizes the cell with new height with smooth animation.
but since this delegate method is called in the completion block of previous animation, it happens after it.
Is there a way to make both of them happen together smoothly ?
Just
1) change the subview height directly without animation,
2) set the new cellHeight of the cell in heightForRowAtIndexPath,
and
3) update the table with animation. If do so, both the subview and the cell will animation correctly.
Use NSLayoutConstraint..so that when a cell changes its height and width automatically its subview will also change its height and width.
NSLayoutConstraint *myConstraint1 = [NSLayoutConstraint constraintWithItem:cell
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:subView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:-5];
NSLayoutConstraint *myConstraint2 = [NSLayoutConstraint constraintWithItem:cell
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:subView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:-5];
[cell addConstraint:myConstraint1];
[cell addConstraint:myConstraint2];
Adjust the constant according to your need.
Hope this will help you.
You ought to make animation in 1 animation function.
When you receive new height start animation(you can use layers transformation)