how to increase the height of scroll viewheight dynamically in swift 3? - ios

How can i set the constraints that here two table views are using so that both are dynamical and i need to change the height of scroll view so that both table views and it needs to fit the whole screen and my layout will be as shown below in the image

why you are using tableview in scrollview ? It is not good approach to use scrollable entity into scrollview! You can take one tableview and can create multiple cell for every type of your content like payment view, cart view etc. You can use multiple section also as per requirement. For example, your Table details should be your first section, that can contain multiple rows.
By this way your height will be automatically managed by tableview.

Related

UITableView cells on the same row

as I say in the title, I'd like to display two or more cells on the same row. I have every cell with a UILabel inside. The labels can have changing width depending on user's input. I'd like to fill a row with many labels as I can. Can someone give me some advice on how to do it? Thanks.
Short answer: You can't. Slightly longer answer: Use a collection view.
Table views are designed to show a single column of cells.
You can't make a table view contain multiple cells in a row.
You could probably create a view controller that had side-by-side table views inside it, but it would be awkward and hard to use, and the table views would scroll independently of each other.
If you want multiple cells on the same row, you want a collection view. A collection view is similar to, but more flexible than, a table view.
I would consider using a horizontal UIStackView to achieve what you're trying to do. Depends on how dynamic and changeable you need it to be.

Two vertical collection views and a label between them to scroll all at once as one element

I have two collection views with data loaded from an API. the data varies that means that the heights of each of the collection views should be varying. I have tried putting all of them ( 2 collection views and a label between them ) inside a scroll view but that doesn't work because I cannot tell the size of the contents before the data is loaded. How can I do this?
For many reasons, this is should not be the layout of choice for you in this situation. First, the collection views cannot determine their own size based on the size of their content, because they are in fact scroll views – and it would be weird for a scroll view to always scale to the size of its content, it would never scroll at all if it did. Second, as each of the collection views is a scroll view, you would have a hierarchy of scroll views which is hard to handle for the user, let alone the developer.
What I suggest instead is to use a single UICollectionView with multiple sections. You can also implement your own UICollectionViewLayout to suit your needs.

How to place UILabel exactly below UITableView?

I have UITableView with height of ≤500. Tableview data comes from database. Below UITableView, there is one label and two radio button. The problem is if data in the UITableView is less than its height, then it shows blank space between UITableView and those two radio button. I want to place those label and radio button exactly below tableview. How should I do this ?
This is how my tableview looks
If the table view's height does indeed vary depending on shown content, you could use UITableView's tableFooterView.
For a bit more context see this post for example.
Alternatively you could do this with basic auto-layout by tying your label and radio buttons (that you place as siblings of the table) vertically to the bottom of the table view.
I could also imagine that you may need a section footer. So you may want to have a look at that too.
If you want it to be scrollable, add it as the last row in your table view. You may also go with table view's footer.
If you want it to be sticky at the bottom in case of more rows, go with auto layouts. You may create outlet of table view's height constraint and adjust that accordingly depending on the number of rows. However, this approach is not appreciated.

How to scroll a tableViewController's view with only static cells inside?

I have a tableViewController which stores a form for the user to input their information. This form is grouped by a couple of static cells instead of dynamic cells. So it doesn't access the delegate method cellForRowAtIndexPath. Also, I have controlled that user can add some of the attachment and keep stacking. These attachments are stored in a single UIView each. The problem is, when more than 6 attachments were added, the attachment view starts to go out of the screen height. How can I make this tableViewController scrollable?
Please tell me if you need me to show any code.
You don't need to set it scrollable.And you should add these views to table footer view and increase its height at the same time.By this,table view will be able to scroll once its height is bigger than that of frame.

What is good practice for code to dynamically add content to View Controller?

I essentially want to have the following scenario.
Very simply, each entry contains a number label, two header labels and two text fields.
Being new to iOS development, I'm not sure of an intelligent, simple way to do this. What is the best way to go about doing this?
This is a great case for using a UITableView. Create a table and a cell prototype that has all the elements that you need (a number, fields for name and age, etc). Create also a different cell prototype for "add more". That would be the last cell in your table. Increment the number of cells when a new record gets added.
There are multiple tutorials online for UITableView. What you want to look for is how to set up and use 2 different cell types. See, for example, this SO question.
If you go with a table view as opposed to just adding some subviews, you get some useful features for free:
scrolling, if you have a lot of records
animated insertion of rows
support for deleting rows/records
easy customization of your list of records: headers, footers, sections.
You can do this manually - there is no problem. Create UITextField, UILabel etc objects in the code, configure them to your liking and add them using
[view addSubview: ]
method
I would:
Create a scroll view - UIScrollView, set the size of this scroll to the size of your main view and add this scroll view to your main view.
Create a content view - UIView, set the width of this content view to the with your the scroll view. But the height should be 0. Set the scroll view in step one's setContentSize: to content view's size. Add this content view as a subview of the scroll view.
Create an iVar to keep track of how many buttons set has been used.
Now every time the + button tapped, create buttons, labels, etc., use the iVar in 3 to calculate and adjust the frame for each buttons, labels, etc.
Adjust the content View's frame to make room for the new set of buttons, labels, etc. Update the scroll view's contentSize: to the content view's frame size.
Add those buttons, labels, etc. in step 4 to content View as subviews.
Case A:
If you are going to add limited number of this type of view
Create one viewController, add textField, label etc to view of this view controller.
Create new instance of this viewController when user clicks add button then use addSubview by refering previously added view's y position
Case B : If number is not limited you will need scroll, then tableView is best option
You will need to create custom cell with your labels and textfield
OR
you can do same using prototype cell if you are using storyboard.
When user will click add button do this
//Update your datasoure for new cell
[tableView insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
You can create add more view and set it as table footer as it will always stay at bottom, like this
table.tableFooterView = assignYourAddMoreViewHere;

Resources