Cannot change height of UITableView - iphone

In the Apple iPhone Clock App, when a user adds a new alarm a modal view pops up. I am trying to create a UI similar to that.
I currently have a UITableViewController as the root view controller of a UINavigationController.
I also have a UIDatePicker added as a subview to the UINavigationController:
[self.navigationController.view addSubview:mydatePicker];
However, I have about 10+ rows in my UITableview (that of the UITableViewController) and as soon as I added the UIDatePicker I cannot scroll to view all the cells.
I realized that the UITableView size is the same size as it was before I added the UIDatePicker, and therefore I would need to change its size in order to be able to scroll to see all the table cells.
I have tried several things in order to change its size, all to no avail. In the code below I arbitrarily chose 50 for the new height.
First, tried changing the bounds height:
CGRect bounds = [self.tableView bounds];
[self.tableView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
50)];
Then tried to change the frame height:
CGRect tvframe = [self.tableView frame];
[self.tableView setFrame:CGRectMake(tvframe.origin.x,
tvframe.origin.y,
tvframe.size.width,
50)];
Then after googling some more i tried changing the contentSize height:
CGSize thesize = self.tableView.contentSize;
thesize.height = 50;
self.tableView.contentSize = thesize;
None of these appeared to have any effect on the size of the UITableView. I still could not scroll to see all the cells.
I later tried some of the same methods as above but on the UINavigationController instead of the UITableView. I didn't have much luck with this either.
As a last resort, I tried to change the size of the UITableView in the Storyboard editor. I could not figure this out either.
Thanks for any help you can provide.

From the UITableViewController class reference, it:
creates an unconfigured UITableView object with the correct dimensions
and autoresize mask
And a quote from the Table View Programming Guide for iOS which specifically addresses this behavior:
Note: You should use a UIViewController subclass rather than a
subclass of UITableViewController to manage a table view if the view
to be managed is composed of multiple subviews, only one of which is a
table view. The default behavior of the UITableViewController class is
to make the table view fill the screen between the navigation bar and
the tab bar (if either are present).
If you don't want the table view controller setting the size of your tableView, then you need to use a UIViewController instead. See the link that I posted above to the Table View Programming Guide for iOS for other things to consider when going this route.

I had same problem as you mentioned.
I was unable to change the height of tableView of UITableViewController. I tried changing frame and centers of self.view and self.tableview. But failed.
Then I solved it by a trick, changing tableview's contentInset.
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44.0f, 0); // here you can replace 44.0f with your height

You need to make the UITableView a subview of your main view in order to change its size reliably.
The frame is the correct value to alter once you have made this change.

I dont know what is your real Problem . but for change hight of your UITableView and will Display all rows in UITableView write following code.
self.tblView.frame = CGRectMake(0, 0, 320, 50);
please try it.

Your are taking uitableview controller, in place of that use uiviewcontroller in that add the uitabelview. Now you can the the size of uitableview.

Related

Uitableview content size issue

I have an table view which content size is more than the uiview size i could not able to see the last cell fully as table view is not scrolling up to the tableview cell .
If you are using autolayout then you need to set bottom space from super view = 0 in your xib. For more info see in this pic.
And if you are not using autolayout then you can set frame direct of your tableview.
Its appear you had set the frame of your UITableView Wrongly, if your UITableView is full screen, set its frame as following:
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
And so on...

UITableview changing height when shown from different UINavigationControllers ('More')

I'm having an issue with UITableViewController's tableView changing its frame position when presented from a UITabBarController, within a UINavigationController. The frame is fine when displayed from a regular tab. However, if I move and show the UITableViewController from the 'More' Section, the tableview moves down the same height as the navigation bar (which I don't want it to). When I tested the runtime frame coordinates between proper and nonproper positions, it shows as the same (0.0, 0.0, 320.0, 411.0).
This issue only happens AFTER I've shown the tableView in one Nav Controller and then it's moved and shown in another, like the 'More' VC to the tab, or tab to the 'More' VC
What's going on here and how can I fix this?
The last image is how it looks like when the app launch, as it should be, with he table view right below the nav bar. In the first two however, you can see that the tableview has been moved down.
Thanks in advance!
I was able to solve the issue. It turns out that when you move a UITableView controller within a UINavigationController to the moreNavigationController, 44 pixels are added to the 'top' variables of contentInset and scrollIndicatorInsets. I'm not sure why this is happening or why those 44 pixels aren't being removed when the tableView leaves the 'moreNavigationController', but here's the answer. I'm calling these two lines from within viewWillAppear.
self.tableView.contentInset = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0.0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0.0);
The UITableViewController has this behaviour that it always try to best fit the screen size. UITableViewController is perfect for fullscreen tableview but otherwize I recommend not using the UITableViewController but instead implement a tableViewControler yourself by implementing both the datasource and delegate. Some did set resizeSubViews to NO and solve their problem but I really advice recreate the controller yourself which is not that much work.
Reset autosizing property of table view nib file.
You can find it inside :
Tool>Attribute Inspector
First take a UITableView in the view using XIB. Then add navigation bar and resize the tableview. You will get what do you actually want.
If you still find any problem then please let me know.
If you can display your code, or take a screenshot of your setup, it would be a lot easier. From my experience, creating UITableViewController instances and hooking them up in Interface Builder will expand to fill the screen.
How are you connecting it to the "More" tab? If it's in two tabs, and you're doing ANYTHING dynamic, check your UITableViewController class viewDidLoad method. You shouldn't have to add / delete / re-add the table unless they're separate instances.
Some places to check:
NSLog your tableHeaderView height and position
NSLog your tableFooterView height and position
Create a backgroundView and assign it to the tableView's backgroundView property to see what it's true dimensions are and where it's being displayed
Check where the UITableViewController's view starting position is.
Wish I could help more. Would like to see your setup / code.

Custom UITableViewCell subview layout

I'm creating a custom UITableViewCell, similar in style to UITableViewCellStyleValue1 but with an editable text field on the right side instead of the detailTextLabel. I'm a little confused on how to size the edit field properly. I want to take the length of the textLabel into account, as well as the size of the contentView, and be able to do the Right Thing when its table view is in edit mode, or if an accessory view (like a disclosure arrow) is added.
But the width of the contentView seems to always be 320 (on iPhone in portrait mode) even for a grouped table. Within my UITableViewCell subclass, I see no way to get access to the table view to which it belongs so I can get the style of the table and adjust the margins accordingly.
I figure I can handle this in a custom fashion in the tableView's delegate -tableView:willDisplayCell:forRowAtIndexPath: method, but that completely defeats the purpose of having a reusable cell class.
I must be missing some key concept here, but I'm at a loss. Anyone?
Thanks!
randy
You may want to take control of your layout in your UITableViewCell subclass. For example:
- (void)layoutSubviews
{
CGRect b = [self bounds];
b.size.width += 30; // allow extra width to slide for editing
b.origin.x -= (self.editing) ? 0 : 30; // start 30px left unless editing
[contentView setFrame:b];
// then calculate (NSString sizeWithFont) and addSubView, the textField as appropriate...
//
[super layoutSubviews];
}
I think the margins are part of the cell, not the table, so that's why the contentView is always 320 wide. Maybe I don't understand the question.

UITableView details as a subview

I have a UITableView in iPhone with enough cell to make it scrollable.
I would like to have a subview display whenever I click on a cell, rather than using the navigation controller behaviour.
The problem is that I cannot calculate the CGRect exactly to have the subview always centered in page, because the CGRect is calculated from top of table, and if I scroll table and click cell, the subview will be added out of screen.
The solution could be easy, but I don't know if it's possible: identify the portion of the current viewable area of the UITableView and obtain in some way the frame and therefore origin and size, then build a subview based on such coordinates.
Do you think it's possible without writing not too much code ?
thanks
Leonardo
A simple solution is to not add your sub view as a subview of the UITableView but of its parent (or the main application window). So instead of doing something like:
[myTableView addSubview:mySubView];
do:
[[myTableView superview] addSubview:mySubView];

UIButton in a UITableView header ignores most touches

I've got a button that I'm adding as a subview of a table view's tableHeaderView. The button appears fine, and tap-and-holding on it works intermittently - for the most part, though, it's unresponsive. I've tried adding it as a subview of the table itself; the effect is about the same. I thought the problem might be with the scroll view's touch interception, but disabling scrolling on the table has no effect either.
Am I doing something wrong? Has anyone else encountered this?
edit - to clarify, I'm talking about the main table header, not a section header, in a grouped-style table; think basically modeled after the "Contact" screen.
I had the same problem. In my case I had a container view instantiated in IB (that was applied as the table view header in code), with a UIImageView occupying the entire frame of that container. The misbehaving button resided in the image view.
Turns out I needed to have sizing struts in IB set as follows...
Container View: exterior anchors all on, interior resizing - all off
Sub Image View: all struts on (interior and exterior)
I had several different table views, all using header views. Some would respond to touch events correctly, some were flaky. This solved my problem
I had a similar problem - a textfield and button inside a view set as the table header view which would not respond to touch events. setAutoResizing programmatically worked for me.
My controller extends UITableViewController, and viewDidLoad looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
MYCustomWidget *headerView = [[[NSBundle mainBundle]
loadNibNamed:#"MYCustomWidgetView" owner:self options:nil]
objectAtIndex:0];
[headerView setAutoresizingMask:UIViewAutoresizingNone];
self.tableView.tableHeaderView = headerView;
}
('MYCustomWidget' extends UIView, and 'MYCustomWidgetView' is a XIB file).
I completely disagree with Wisequark -- there's absolutely nothing wrong with putting a button in the tableHeaderView, and including one would not risk your app being rejected from the app store. The tableHeaderView is designed to be an arbitrary view containing whatever elements you choose.
As far as your issue, it could be that you've got a view obscuring your button, or, it may simply be a bug that should be reported to Apple.
Strangely enough, but the table header view is apparently resized incorrectly.
I use auto layout, so autoresizing mask was not an option for me. After inspecting my view hierarchy:
and noticed that my custom header view had incorrect height, so only less then half of it was tappable (see highlighted view):
Manual updating of its height fixed the problem:
- (void)viewDidLayoutSubviews {
CGRect frame = self.tableView.tableHeaderView.frame;
frame.size.height = 116.0;
self.tableView.tableHeaderView.frame = frame;
}
Also, the table view header height can become invalid after the orientation is changed. This problem also can be fixed with the provided solution.
My situation was similar to Danny Hall's (the table header view was a UIImageView, and there was a UIButton which was a subview of the UIImageView). In my case, the problem appears to have been caused by the button being a subview of the image view. What worked for me was creating a UIView "containing" view, such that both the image view as well as the button were subviews of the "containing" view. strange.
tableHeaderView has 0 height while it is processing draw in UITableView
use this UIView subclass to set the strong constant height and ignore UITableView processing
#import <UIKit/UIKit.h>
#interface CustomHeaderCell : UIView
#end
//-----
#import "CustomHeaderCell.h"
#implementation CustomHeaderCell
-(void)setFrame:(CGRect)frame {
frame.size.height = 43; // !!! constant height
[super setFrame:frame];
}
#end
I have the same problem UIButtons actions not working in UITableView's header view. First i tried setAutoresizingMask to .None which not works then after reading the answers of #Davyd and #Alexey i realise that i did not set the height of headers view then i set it like:-
self.tablevwMain.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: width_view, height: your_height)
And all UIButton'sinside UITableView's header view works correctly.
For me UIControl like UIButtons on headers only worked if I add it to the cell's contentView
addSubview(stackView) //Does not work
contentView.addSubview(stackView) //Works
Don't forget to set the footer height in:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
You should consider that this is not the intended sue of the headerView and that an implementation such as that might result in rejection from the AppStore as a result of a HIG violation. Given that the dimensions of a header are intended to be small, it is probably better to consider a restructuring of your view. Having said that, there is no easy way to do it short of hand detecting touch events and determining the geometry yourself, then executing a selector based on the geometry - in short, rolling your own button class.