Custom UIView(.xib) changes frame automatically after adding as subview in UITableViewCell - ios

Due to requirement, I have made a custom UIView with .xib. The problem is when i alloc,init the uiview , the frame is all right but when i add the view as subview to UITableViewCell.contentView, the frame of my custom view dramatically changes. I even tried to enforce the frame after adding as subview but of no help.
I am using storyBoard with autolayout. the problem persists in both case when i set constrains in my custom view and when remove all constrains .
Thanks in advance.

So your issue is that you are not changing height of row when your adding view into your cell's content view. Here is below height method and changes that you need to implement:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((indexPath.row==3 && rowSelected)) //You need to put condition check in which you need to see if that particular row is selected or not.
//Then a flag for checking if view is added or not to it's contentView.
//If both condition true then you need to provide a new height to that particular cell.
return 220; //120 height of your cell + 100 height of your view.
else
return 120;
}

Related

UI Tableview cell not expanding when text is added at the runtime

I am using table view for my chat application, in which the chat responses are added either one of two textviews as shown in the image. The thing is whenever I add a large text, the cell height does not expand to show the full text. It only shows the last line of that text. I am using objective-c. Is there a way to solve this ?. I do not want to completely create a new view like the questions asked before, I want changes to this specific view.
First, you need to use Label and set top and bottom constraint between label and contentView cell.
In order to allow the self-sizing cell mechanism to work, you must set the rowHeight property on the table view to the constant UITableViewAutomaticDimension. Then, you simply need to enable row height estimation by setting the table view's estimatedRowHeight property to a nonzero value, for example:
_tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
_tableView.rowHeight = UITableViewAutomaticDimension;
You need to defined self-sizing cell like this.
tableCountries.estimatedRowHeight = 100.0;
tableCountries.rowHeight = UITableViewAutomaticDimension;
Make sure you have a proper constraint. You can have a constraint like this.
In the below label you need to set your textView top position. That's why when the below label expands it automatically expand label and text view will push to bottom.
I think your cell is not gettings its height because of textview scrolling maybe that will help:-
Use UITableViewAutomaticDimension and for that your tableview cell content should justify both top-bottom leading and trailing constraints to calculate cell height.
Set scrollEnabled to "false" for your textview.
With everything on the place your cell will get its height for sure.
you can do it in many ways :
1) You can set dynamic height of cell using UITableViewAutomaticDimension.
You need to set proper constraints in storyboard like setting top and bottom.
in ViewDidLoad add below :
tblView.rowHeight = UITableViewAutomaticDimension
tblView.estimatedRowHeight = 100
and in tableView cell's height method :
return UITableViewAutomaticDimension
2) Find text's height dynamically using below function :
let width = 100.0 \\your text label's width you gave
var height = chatMsg.text?.height(withConstrainedWidth: width, font: chatMsg.font!)
using above code you can find the height of your text. and you can use it dynamically
3) there are many code available to use for chat application in which you just need to pass text/image, and it will handle everything else.
Link : https://github.com/kerry/iMessageBubble
Add these two delegate methods:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
Create Custom UITableViewCell with Three UILabel like lbl1,lbl2,lbl3.
Add the AutoLayout Constraints to lbl1 left, right, top and bottom. With fixed height (30).
Then set same constraints to remaining two Labels.
Here is main things:
1.select lbl1 heigh and double click on height constraint and set >=30
Do same things with lbl2 and lbl3.
Final step is:
Select lbl2 top constraints, change the constraints property like <=8.
Do same things with lbl3, that's it we are done all.
thanks..

iOS 8 : how to set height programmatically for a UITableview in a freeform ViewController

Rookie in iOS.
I have a Freeform ViewController, which has an UIImageView and
UITableView in it.
The Problem i am facing here is when i try to scroll the tableview
some of the items stay hidden because of the height it has taken in the freeform
viewcontroller.
I tried the following code to set the UItableview's height programmatically
according to the size of the screen.
let screenSize: CGRect = UIScreen.mainScreen().bounds
tableview.frame = CGRectMake(0,imageview.frame.height, 280, (screenSize.height -imageview.frame.height))
The above code failed to resize the tableview.
Is there any other way that i can set the UITableview size at Runtime or
in the storyboard.
Solution
I resolved the issue after laid out my both views using custom height and width after specifying translatesAutoresizingMaskIntoConstraints = true for both the views. I did as per my design and app requirement.
if you are using auto layout or constraints, you have to
tableview.translatesAutoresizingMaskIntoConstraints = true
before applying the new frame, the new frame is not updating because it's getting over-riden by auto layout and constraints.
EDIT :
1- if you have [adjust scroll view insets] enabled in your view controller in story boards disable it, and test and see.
2- do you create the imageView programatically? or from interface builder, and do you have constraints on it?
3- did you know that the status bar has a height and you have to compensate for it too?
4- do you use a navigation bar?
There is a delegate function for the UITableViewCell height.
Here you specify the indexPath of that particular cell and return your height for it
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == yourSection && indexPath.row == yourRow) {
return 140.0; //return height for required row
}
// "Else"
return someDefaultHeight;
}
Don't set Tableview height programmatically.
Just do one thing
Take Top, Bottom, Leading, Trailing constraints and set constant value to 0
Add user image view into tableview header.

seprator is not visible in uitable view for dynamic height?

I am designing an app in which i have used a table view.This table view uses a custom cell.I have given proportinal height to the table view.T
tableview height constraints:
equal height to mainview
multiplier:189:568
Cell properties
cell height:77
Image constraints:
Label constraints
TextViewContsraints
bottom right label constraints
Code to make the row height dynamic
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
float percentage = (77.0 / 568.0);
float height = self.view.frame.size.height * percentage;
return (height>77.0)?height:77.0;
}
Issue screen
Is the UITableViewCell in IB associated with a UITableViewCell subclass that overrides drawRect:? If so, make sure you are calling the super implementation, as that's what draws the line at the bottom. If you are overriding layoutSubviews make sure no views are obscuring the bottom of the cell.
In my case, i forgot to call [super layoutSubviews] after overriding the layoutSubviews method. Took me hoursto find the problem.
Hope this will help you.
Increase custom cell size to 80-88 may be it solve your problem

Change height of the "header view" inside UITableView

I added a view "Header View" inside a table view (see structure in screenshot). It has fixed height. I tried to change view's frame size, view is getting smaller, but there is still empty space between header and tableview cells.
I need to change header height dynamically in code. Any suggestions?
I suggest you move the Header View out as a subview of View, set a tag to it, then in viewForHeaderInSection, use the tag to get it. Set the proper height in heightForHeaderInSection. When height changes, refresh tableView. The reason for that is Header View is no longer a subview of the table view, it will always stick to the first table view row.
Also with the current design you can make the Header View a cell, then in the viewForHeaderInSection you can reuse the cell, if you have multiple sections this is the way to go.
In your tableview's delegate, you have to use
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return /*desired height*/;
}
EDIT
According to your comment, if you don't want to change your interface, what you can do is to set a height constraint to that view.
You then make a reference of the constraint to the header of the controller managing the view.
In the code, you can then change the height by changing the value of the constraint and updating the view :
[UIView animateWithDuration:/*animation time in seconds*/ animations:^{
yourHeightConstraint.constant = /*desired height*/;
[self.view layoutIfNeeded];
}];
[self.view updateConstraints];

How to remove header and footer space from a UITableView?

I have a UITableView in the grouped style, and only one section. However there is some blank space above and below the table view that is shown when the user scrolls too far. How can I remove this blank space?
You can do this by altering the contentInset property that the table view inherits from UIScrollView.
self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);
This will make the top and bottom touch the edge.
Add this code:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
Actually this question answered my question.
Reducing the space between sections of the UITableView.
UIView can be inserted at the top and bottom of the table(drag and drop). Set their properties as transparent and height of 1 px. This is to remove the extra padding in front of the cells.
you can also use this code for removing space between first cell of uitableview..
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.002f;// set this...
}
Uncheck Extend Edges Under Top bar.
This answer comes quite late, but I hope it helps someone.
The space is there because of the UITableView's tableHeaderView property. When the the tableHeaderView property is nil Apple defaults a view. So the way around this is to create an empty view with a height greater than 0. Setting this overrides the default view thereby removing the unwanted space.
This can be done in a Storyboard by dragging a view to the top of a tableView and then setting the height of the view to a value of 1 or greater.
Or it can be done programmatically with the following code:
Objective-C:
CGRect frame = CGRectZero;
frame.size.height = CGFLOAT_MIN;
[self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:frame]];
Swift:
var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)
Comments
As others have noted you can use this same solution for footers.
Sources and Acknowledgements
See the Documentation for more details on the tableHeaderView property.
Thanks to #liushuaikobe for verifying using the least positive normal number works.
My original answer: https://stackoverflow.com/a/22185534/2789144
In my case issue was with the constraints i was applying. I have to change them in order to show 2 rows in my case while bottom of table touching last row.
Use the bounces property of UIScrollView:
[yourTableView setBounces:NO];
This will remove what seems to be an extra padding at the top and bottom of your UITableView.
Actually, it will just disable the tableview's scrollview to scroll past the edge of the content.

Resources