Placing a button at the bottom of a collectionView - ios

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

Related

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.

animation not correct with implicit autolayout

I upload my demo code here:https://github.com/liuxuan30/Problems
The main problem is, I had a view which have a scroll view inside, and there is a label and collection view inside scroll view.
There will be a unread message button generated by code, when there is unread messages, the button will pop out, and I expect the animation: label and collection view will down-shift by the button's height.
When I test the animation without adding the button in subviews, animate as expected.
When I add the button in, it seems like the label and collection view's origin.Y up-shifted, and start the animation. Turning off auto layout will solve it, but I have to have auto layout. I tried to add constraints for all views, but the animation still not work out.
You can try comment out
[self.HomeScrollView addSubview:AlertView]; and self.UnreadAlertView.alpha = 1.0f; inside the code to see the animation.
Hope someone could figure out where i did wrong.
UICollectionView is already a subclass of UIScrollView. Embedding your collection view inside a scroll view may be what is causing problems with your animation.
Try removing your scroll view and just put your UIButton, UILabel, and UICollectionView inside your overarching view.

Why does UIButton alter subviews' positions?

I add 2 label and 1 image to show at specific positions in a button.
If I don't reset positions inside layoutSubviews method, button breaks all those positions. (for example sticks the image to 0,0)
If I add a parent contentView for all the subviews and add contentView to button, it doesn't change anything either.. button breaks positions inside the contentView too.
What can be the reason of this?
If this is not working you can go for the custom view.
1) Add custom UIView.
2) Add your labels and images on it
3) Add TapGestureRecognizer to the custom view.
This will give you button like functionality, and all images and labels will be at proper position.
Hope this will help you.

Anchor a UIButton to the bottom of a UITableViewController's view

I have the following requirement. When a UITableViewController's view is displayed, there are a variable number of rows. Underneath the rows, a button should be displayed.
When the number of rows is small, the button should be anchored to the bottom of the view.
When the number of rows is larger, the delete button should be placed immediately after the last row.
In other words:
And not:
My best attempt at this so far has involved setting a tableFooterView and trying to update its height using the contentSize of the UITableView, but I am running into all sorts of problems. I might continue down this path and ask for some help, but first I want to know if anyone has alternative (better) solutions.
The result must play nicely with a double-sized status bar (during a call for example) and I am targeting iOS 6.0. I am not using interface builder.
One possible solution to achieve this effect might have to use two different solutions.
If the amount of rows means that the button will be off the screen then use the footerView like you have been doing.
If the amount of rows means that the button will not be off screen then
Add the button to the tableView
Implement - (void)scrollViewDidScroll:(UIScrollView *)scrollView and update the frame of the button to be offset from the bottom.
The offset from the bottom might follow some logic like this
yOffset = CGRectGetHeight(tableView.frame) - (CGRectGetHeight(button.frame) + somePadding)
yOffset += tableView.contentOffset.y
This would mean that the button still moves up and down with the scrolling but you don't have to mess with the footerView height
Keep both the table view and a button inside scroll view. Keep the button at the bottom of the scrollview. For proper scrolling to work you might want to set the scrollEnabled property of the scroll views. For more details on that check this up
Scrolling a UITableView inside a UIScrollView
EDIT:
yourView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin ;
Check the accepted answer for this question for more details on implimenting struts and springs using code:
UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C

Resources