Create a data entry form with scrolling like Contacts form - ios

I am using a storyboard, to create a View on which the user can enter data, like creating a contact.
I have tried placing some UITextFields on a view, but this way, I have the problem that there will be no scrolling when the iPhone is showing the keyboard at the bottom.
I hope someone can give me some hints about which controls I have to use to create a data entry form like the one for entering contacts in the iPhone.

You need to place an UIScrollView to your UIView element and then you can add UITextField objects to the UIScrollView as you wish. After doing this, you need to make an IBOutlet property (e.g. scrollView) for your UIScrollView and link them.
Finally, set the content size of your UIScrollView in the viewDidLoad method. Something like this:
_scrollView.ContentSize = CGSizeMake(320.0, 640.0);
Please note that the first number means the width and the second means the height of your scroll view's content and you can change them depending of your wishes.

You have to use UITableView, with cell where you put UITextField's. When user tap on textField you have to register notify of keyboard appear (some hints there keyboard notification ) and change tableview frame to adjust to keyboard.

Related

Placing a button at the bottom of a collectionView

I'm trying to make a collectionView that shows 10 item at first, when scrolling to the bottom I want to have a button that clicking it will load more info, its something simple but the thing is that I'm not using storyboard and I did everything programmatically, so how it can be done?
UICollectionView is a subclass of UIScrollView. So you need to:
Add your button as subview of your UICollectionView
Set it's frame in the way you prefer (if you count your frames in code - just set it manually) based on .contentSize of you UICollectionView.
Set .contentInset.bottom property based on the height of your button/footer
You need to handle the case when .contentSize changes. If your button should be visible all the time, maybe better to set your button frame in viewDidLayoutSubviews() function
If you want to show/hide it in some cases, you can animate it using UIView.animate(withDuration:) by setting .contentInset.bottom in the block

Can I make only a certain portion of a tableview clickable?

I'm trying to add a like button in a tableview but If I click anywhere in the tableview, it immediately goes to the detailsVC. I want to make it so when you click that like button, only that is selected. Is there a way to make it so only the top half of the tableview can be selectable or have interaction enabled, and put the button below? Or is there another way to do this. Is there a workaround or a method I am missing. Thanks
Are you sure the UIButton is added correctly to table view cell's .contentView?
Can you see it? If so it will intercept the touch events and tableview's delegate didSelectRowAtIndexPath won't get called (even if there's no handling connected to the button). If you want make the button occupy specific area of the cell you can either:
Design your cell in Interface Builder and make it a part of Storyboard/Xib
Do autolayout of the cell programmatically
Override cell's layoutSubviews and manually setup the frames

Swift, iOS - have button move up when clicked to display text field? Then move back?

I know how to accomplish this in something like sprite kit but with just a normal ViewController I'm a little lost - right now I have my button and my UITextfield set up on my xib file. Right now, when the button is clicked my textfield goes from hidden to not, which is great.
Problem is I need to have my button slide up (as in animate up) to "reveal" the text field when clicked. As in the textfield would be underneath the button. When another button is clicked, I want the button to slide back down to its original position. I don't know how to implement these animations.
Here is an image of what I need:
So the blue button would trigger the move up when clicked. Then if another button is pressed the blue button moves back to original position over textfield.
How can I accomplish this?
Here's an option you can try if you aren't displaying these in a UITableView:
Setup your .xib with all of the possible views in an 'expanded' state. Give your views that will be revealed a height constraint (in addition to the others needed to layout the view properly), and then create an outlet for those height constraints in your view controller. Additionally, store a reference to what you want the expanded height of the view to be in your view controller.
In viewDidLoad of your view controller you will set those height constraints' constant values to 0. This will cause these views to effectively disappear, and your other views should fill in the gaps they leave behind based on their constraints.
When one of your buttons is pressed, you'll update which views should be visible by setting their height constraint to 0 if you want them hidden, or to the value you saved earlier if you want them shown. After the height has been updated, call
UIView.animateWithDuration(howLongTheAnimationLasts) {
self.view.layoutIfNeeded()
}
which should animate the change in height values.
Alternatively, you could try putting your options in a UITableView and altering their height using tableView:heightForRowAtIndexPath: and reloading the cells when their heights should change.

Button not responding in Table View Cell [duplicate]

This question already has answers here:
Programmatically send to front/back elements created from interface builder
(2 answers)
Closed 10 days ago.
I am using a .xib for my cell in a table view. Inside the .xib, is another view (cellBackgroundView), and a button. When I run the app, and click the button, it does not respond at all. Instead, it calls the tableView's didSelectedRowAtIndexPath method which brings another view controller.
Using Xcode's Debug View Hierarchy, I discovered that I have a view overlaying the all the buttons (see pic attached: this overlaying view is highlighted). This view (called backgroundView) that is overlaying my button is a View, within a view. I have a feeling when you place a view in a view, and put a button in the initial view, the button isn't called because its below the view hierarchy.
How do I fix this issue? Is there a way to move background view to the back of the view heirarchy so that the buttons will be responsive?
Debug View hierarchy:
Structure of .xib
Two things that you could check
1) Do you have a delegate method for cell height and is the height returned correct? Unless you have Clip Subviews on for the UITableViewCell, the contents of the cell can be visible outside it's frame, but the parts that are outside the cell's frame are not registering user interactions.
2) Is some other view element higher in the hierarchy (lower in the XCode listing you posted) overlapping the button? iOS Simulators Debug -> Color Blended Layers can help spotting this.
Edit:
If I interpret the added screenshots correctly, you probably have the issue mentioned in the option 1) above. If the other elements showing in the screenshot are those listed as subviews of the Cell Background View they are mostly outside the parent view's frame and thus don't receive touch events. If the background view's frame is correct, then you might want to move the other elements as children for Feed Cell directly.
Also, the element listing suggests that you are using plain UIView as the parent element. I don't know the inner workings of your application, but if you only use this view in a UITableViewCell you might want to consider making the parent view a Table View Cell in the xib. This will reduce some bloat and allow you to configure some properties for the cell in the xib.
Maybe you forgot assign your Button to code
I assume Feed Cell is a subclass of UITableViewCell, and cellBackgroundView is the property contentView of this cell.
If so, the cells property backgroundView should be behind your cellBackgroundView (the docs say: UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.).
You could set the cells property backgroundView = nil, and see whether it is still there in the view hierarchy. If so, you do add a custom backgroundView on top of the other cells views somewhere.
To check this, you could read out the subview hierarchy of your cell in your method tableView:cellForRowAtIndexPath: with something like NSArray *svs = cell.subviews; (assuming cell is the tableViewCell) and set a breakpoint behind this statement.
svs usually contains first the UITableViewCellContentView, and above it the _UITableViewCellSeparatorView. The cells backGroundView will not be shown. You could check there your view hierarchy.
If by chance there is a custom backgroundView on top, you could - as a workaround, not a solution - bring the contentView to the front by sending to the cell bringSubviewToFront: with the contentView as argument. Then the button should respond.
In your Structure of Xib Place your button below the view that is first the view is added to superView then the Button, then your button will work.
or you can code
-(void)viewDidLayoutSubviews{
[self.view insertSubview:yourButton aboveSubview:cellBackgroundView];
}
Hope it will help.
Do you add an UIButton by code?
If so you should ensure you addSubview: into cell.contentView and not into cell.
Also you could try to apply CellBackground class to a view inside contentView, not directly to contentView.
you can use this method.
[cell.contentView bringSubviewToFront:yourButton];
after that if you want to back in background then tou can use sendSubviewToBack: method .
After adding this methods your button is not responding set the userIntractionEable of button's superview.

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