TextField subview not resizing w/ UITableViewCell frame size change - ios

Have a storyboard w/ a TableViewController that has a grouped table view. In the first section, I want the cells' width to be smaller than full-screen. To accomplish the latter, I have a custom UITableViewCell class w/ the following method:
- (void)setFrame:(CGRect)frame {
frame.origin.x += NAME_TABLE_VIEW_INSET;
frame.size.width -= NAME_TABLE_VIEW_INSET;
[super setFrame:frame];
}
That works fine. The issue I have is that a UITextField subview that I dragged into the storyboard cell does not adjust its width automatically to the new cell frame size.
I've tried sub-classing UITextField and ensuring that the autoResizingMask is set properly, and I've tried using [super layoutSubviews] in the setFrame method above. None of these approaches works.
Any suggestions on how I can get the text field to adjust its width automatically while still using this storyboard approach?

The solution for this was to add a separate table view in the header of the tableView provided by the table view controller. Specifically, add a view object to the top of the table view in IB; add a table view as a subview into that view; change the width of the new table view's cells.
Note that this problem exists only because I wanted to use a table view controller, which defaults each cell to the width of the screen. A view controller could have been used, but then you cannot add a table view w/ static cells as a subview.

Related

How to stop a UIView from scrolling with tableview?

I have a UITableViewController and I put a UIView right under the navigation item and above the actual table. The problem that I have is that the view scrolls with the tableview.
How would I get it to behave exactly like the nav bar, and have the items in the tableview scroll behind it.
Rather than having the view scroll, it should remain in its position and have everything go behind it. Sorry for reiterating, but I've found thats necessary sometimes.
The view you're placing above the cell in the storyboard becomes the table view's tableHeaderView.
You can make the header view appear fixed by resetting its frame.origin to the table view's bounds.origin every time the table view lays out its subviews:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIView *header = self.tableView.tableHeaderView;
CGRect frame = header.frame;
frame.origin = self.tableView.bounds.origin;
header.frame = frame;
}
Result:
Assuming you don't want the map view to move then you could set its user interaction to false.
Alternatively you could set the header of your tableView (if you only have one section) to the map view.

How to clip table view cell to view above

I have custom tableViewCell and View above it in tableView. After taping on button views height increase and cover cell's view. How to clip cell to bottom of view for changing position of cell and don't allow view to cover cell?
I tried to override setFrame: method in my cell. But it doesn't work.
If the view above the cells is a table header view, you should reset that view as the header after you increase its size. So, if you're view was called myHeader, you would do this after changing the size ,
self.tableView.tableHeaderView = myHeader;

iOS Storyboard: how do you edit a content view larger than screen in a UIScrollView

I have a view that is larger than screen, I need to put it in a UIScrollView.
So I first add an UIViewController to story board, then, I add a UIScrollView to the root view of my view controller, then when I add subviews of UIScrollView, but I can't add them outside the scrollview area I can see, how to solve this problem?
Change the simulated size of the view controller to Freeform and then you can set the size to whatever you want. After editing, you may want to set it back.
Edit 1:
1) put your subview into scrollview's visible area
2) change frame of the new subview as you wish
3) change contentSize property of scrollview with runtime attributes
Edit 2:
u can create a new view with xib, which contains all your subviews, and then add this view on scrollview OR u can use storyboard's Container View like this:
p.s.: don't forget about contentSize (point 3 in my first edit), but if you are using auto-layout, you need to set it programmatically like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentSize = CGSizeMake(320, 250);
}
put a scroll view in the view controller ,and put a view inside the scroll view named containerView.
design the big content in another view controller (named bigContentView)
in code ,view load event ,
a. get the bigContentView by storyboard Id. ( get the controller and use controller.view)
b. create the outlet to the containerView.
c. update the containerView's frame according your requirement.(update the width and height)
d. bigContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
e. containerView addSubView:bigContentView.

Scrolling ViewController Content

What I have in the view controller view are :
An image of fixed height
Few labels
Table view with n rows.
Once rendered I want everything here to be inside the scroll the view so the user can scroll the entire screen as needed. Note that the scrollView needs to expand to the entire size of the tableView to show its full contents. I have tried different ways of doing this but unable to do it. I would appreciate any pointers or code segment to get this done.
There are essentially two ways to do so.
tableHeaderView
The first way involves the tableHeaderView property of the UITableView instance you have. You can simply add the UITableView with the constraints/frame/autoresizingMask that allows you to put it full-screen. Done that, you simply do (i.e. in your viewDidLoad):
UIView *headerView = [UIView new];
// Here I am supposing that you have a 200pt high view and a `self.tableView` UITableView
headerView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 200.0);
UIImageView *fixedImageView = [UIImageView new];
// configure your imageView..
[headerView addSubview:fixedImageView];
// configure labels as you want and add them to headerView as subviews
// Now set `UITableView` headerView
self.tableView.tableHeaderView = headerView;
If you want to use AutoLayout for your tableHeaderView, I suggest you to take a look at this question
Dynamic scrollView
Another way to do this is to to create an UIScrollView, put everything inside, and let it scroll. The downside of this method is that if you are using floating section headers for your UITableView, they will not float due to the fact that the tableView is going to stay fixed, while the parent scrollView is going to scroll.
On the other side, this approach is more AutoLayout friendly due to the fact you can use constraints easily.
To do so, you start adding an UIScrollView to your view, and placing all your other views inside it.
Be sure to add a Vertical Spacing constraint between the first view inside your scrollView (I suppose the UIImageView) and the scrollView top, and between the last view (I suppose the UITableView) and the scrollView bottom, to avoid an ambiguous content size.
You should have something like that (I omitted the labels for the sake of brevity):
Note that every view is inside a parent UIScrollView
After that, add an Height constraint to the tableView, and add an IBOutlet to your view controller subclass, i.e. like this:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
Now you only need to configure this constraint to reflect the tableView natural height, given by its rows, etc. To do so, you simply calculate the height in this way:
// Resize TableView
CGFloat height = self.tableView.contentSize.height;
self.tableViewHeightConstraint.constant = height;
Now the tableView will resize, and due to its constraints it will adapt the parent scrollView contentSize.
Just be sure to refresh this height constraint anytime you reload the UITableView dataSource.

iPad - UITableView with UITextField - Autoresizing problem when orientation changes

In my iPad split view application, I've a table view in detailed view as well. Each row has a text field. When orientation changes, the table field in table view is extending out of table view.
Can some one tell me how to set the auto resizing mask so that even when orientation changes, the text fields will be within the table view?
if you add tableview as subview of UIView then:
[self.view setAutoresizesSubviews:YES];
[tableView setAutoresizesSubviews:YES]; or set in IB
while creation of cell set like this:
[urcell setAutoresizesSubviews:YES];
if u add textField as subview of cell. So that it will handle all of its subviews while orientation

Resources