I'm trying to modify a UICollectionView using a UICollectionViewFlowLayout to support some parallax effects when scrolling. I am able to do this on the UICollectionViewCells by implementing something like:
- (void)scrollViewDidScroll:(UIScrollView *)mainScrollView
{
CGPoint offset = [mainScrollView contentOffset];
for (UIView *cell in [self.mainCollectionView visibleCells])
if ([cell conformsToProtocol:#protocol(KSParallaxCell)])
[((UIView <GSParallaxCell> *)cell) parallaxInScrollView:mainScrollView toX:offset.x toY:offset.y];
}
Is it possible to iterate over the supplementary views (UICollectionElementKindSectionHeader and UICollectionElementKindSectionFooter) to check if they support the same parallax delegate?
Forgot to check obvious of looping through subviews instead of calling the visible cells:
- (void)scrollViewDidScroll:(UIScrollView *)mainScrollView
{
CGPoint offset = [mainScrollView contentOffset];
for (UIView *cell in self.mainCollectionView.subviews)
if ([cell conformsToProtocol:#protocol(KSParallaxCell)])
[((UIView <GSParallaxCell> *)cell) parallaxInScrollView:mainScrollView toX:offset.x toY:offset.y];
}
Related
I have a UITextView in a UITableViewCell. They both dynamically size itself to fit. The problem I have is that when the texview reaches below the bottom of the screen (from making new lines), the tableView does not scroll to its location.
I tried this code, but it didn't work.
- (void)textViewDidChangeSelection:(UITextView *)textView {
[textView scrollRangeToVisible:textView.selectedRange];
}
You can find the cell containing the textView and tell the table view to scroll to that cell:
UIView *parentView = textView.superview;
while(![parentView isKindOfClass:[UITableViewCell class]]) {
parentView = parentView.superview;
}
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell*)parentView];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
You should add code below to scroll the tableView too:
- (void)textViewDidChangeSelection:(UITextView *)textView {
[textView scrollRangeToVisible:textView.selectedRange];
// since you are only scrolling the table a little bit and not by a whole cell, then change the scrollView.offset
CGPoint offset = tableView.contentOffset;
offset.x += 20; // an example. You need to change this based on the new size of the textview.
tableView.contentOffset = offset;
}
The new 'swipe to delete' look and feel in iOS 7 added a 'bounce' effect where the UITableViewCell continues to offset after a swipe. Is there any way to disable this bounce, so that the cell makes a hard stop once the delete button is fully visible?
Cell that continues to offset:
I want the cell to stop here even if dragging continues:
I tried this in my cellForRowAtIndexPath: method, but nothing seemed to change.
for(UIView *subview in cell.subviews){
if([subview isKindOfClass:[UIScrollView class]]){
UIScrollView *theScrollView = (UIScrollView *)subview;
theScrollView.bounces = NO;
}
}
I think I finally found a solution! Using a custom cell, you can set that cell as a UIScrollViewDelegate and implement the scrollViewDidScroll: method. In that method, you can force the UIScrollView's contentOffset to stay under a particular value (I'm using 82.0f because that seems to be the contentOffset when the 'Delete' button is fully visible). Like this:
.h
#interface MyCustomCell : UITableViewCell <UIScrollViewDelegate>
.m
-(void)awakeFromNib{
[super awakeFromNib];
for(UIView *subview in self.subviews){
if([subview isKindOfClass:[UIScrollView class]]){
UIScrollView *theScrollView = (UIScrollView *)subview;
theScrollView.delegate = self;
}
}
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
static CGFloat kTargetOffset = 82.0f;
if(scrollView.contentOffset.x >= kTargetOffset){
scrollView.contentOffset = CGPointMake(kTargetOffset, 0.0f);
}
}
This can also be done without using a custom cell by simply setting a ViewController as a UIScrollViewDelegate and setting the UIScrollView's delegate in tableView:cellForRowAtIndexPath like so:
.h
MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
for(UIView *subview in cell.subviews){
if([subview isKindOfClass:[UIScrollView class]]){
UIScrollView *theScrollView = (UIScrollView *)subview;
theScrollView.delegate = self;
}
}
return cell;
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
static CGFloat kTargetOffset = 82.0f;
if(scrollView.contentOffset.x >= kTargetOffset){
scrollView.contentOffset = CGPointMake(kTargetOffset, 0.0f);
}
}
I dont think there is an option for that but perhaps what you can do is subclass your cell and in didTransitionToState: you can detect the delete confirmation state.
Now at this point im not entirely sure what you can do to prevent the scrolling but I hope this puts you in the right direction.
Maybe you can disable the cell's gesture recognizer in this state?
try this for your custom delete button
https://gist.github.com/JigsChanchiya/7002903#file-customswipetodelete-mm
I have a split view controller: view & tableview. In this table view I have custom cells with text field. I don't know how many cells will I have, so it generate automatically. And now I'm trying scroll to textfield, when it becomeFirstResponder. I've tried something like this:
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGPoint focusOnTextField = CGPointMake(0, 300 + textField.frame.origin.y);
[scroller setContentOffset: focusOnTextField animated: YES];
}
300px - start position of my TableView. All seems alright, but textField.frame.origin.y always equal to 0 (like and bounds.origin.y btw).
I thought I can solve a problem if get position of cell, which textfield is active, and then replace textField.frame.origin.y on cell.frame.origin.y or something like this.
===================================================================
I forgot say that my tableviews scroll is disabled. I follow your advices and code examples and solve it like that:
- (UITableViewCell *)cellWithSubview:(UIView *)subview {
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *activeCell = [self cellWithSubview:textField];
float offsetValueY = 200 + activeCell.origin.y;
CGPoint focusOnTextField = CGPointMake(0, offsetValueY);
[scroller setContentOffset:focusOnTextField animated:YES];
}
And know what? It's working! :-) But it create a new problem. When I begin editing textfield, scroller at first jump on top, and only then going to it correct position. When I write [scroller setContentOffset:focusOnTextField animated:NO]; and this problem disappear, but there is no smooth move of scroller. And this is bad to me :-) So how we can solve it?
Here's how to scroll to a cell containing a text field ...
// find the cell containing a subview. this works independently of how cells
// have been constructed.
- (UITableViewCell *)cellWithSubview:(UIView *)subview {
while (subview && ![subview isKindOfClass:[UITableViewCell self]])
subview = subview.superview;
return (UITableViewCell *)subview;
}
Your idea is correct to trigger that action when editing begins. Just do it using the cell...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// find the cell containing this text field
UITableViewCell *cell = [self cellWithSubview:textField];
// now scroll using that cell's index path as the target
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
If you add the textfield in the UITableVieCell content view (added by default if you are using .xib) then you have to call something like textField.superview.superview and this will give you the parent cell. If you add the text field directly to the cell view then you have to textField.superview.
[tableView scrollToRowContainingComponent:textField atScrollPosition: UITableViewScrollPositionMiddle animated:YES];
after adding the following category to UITableView:
#implementation UITableView (MyCategory)
-(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
if([component isDescendantOfView:self] && component != self) {
CGPoint point = [component.superview convertPoint:component.center toView:self];
return [self indexPathForRowAtPoint:point];
}
else {
return nil;
}
}
-(void)scrollToRowContainingComponent:(UIView*)component atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
NSIndexPath *indexPath = [self indexPathOfCellComponent:component];
if(indexPath) {
[self scrollToRowAtIndexPath:indexPath atScrollPosition: scrollPosition animated:animated];
}
}
#end
I have composed a tableview with some custom cells. Some of the custom cells contain UIWebView, some has UISlider plus UITextField, some has UIButton.
Now when I click on textfield which is uitableviewcell's subview at the bottom of the UITableView, I can't manage to get my table cell properly visible above the keyboard.
Using of UITableViewController instead of UIViewController is not possible for me. Because my UI structure is somewhat weird and dynamic. In simple way it is like
UIView -> UIScrollView with paging -> x number of UITableViews -> y number of UITableViewCell -> z number of UITextFields, UISliders, UIWebViews, UIButtons, UILables, etc.
I have also tried below code to make it work.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
but it is not working :(
Thanks,
EDIT 1:
I have checked references for textfield, tableviewcell and tableview. So reference of object is not the problem.
Try this code....
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
NSIndexPath *indexPath = [[table_Question[pageNumber] indexPathForCell:cell];
int totalRows = 0;
if([[table_Question[pageNumber] numberOfSections] > 1)
{
for(int i = 0; i<([indexPath.section] - 1);i++)
{
totalRows += [[table_Question[pageNumber] numberOfRowsInSection:i];
}
}
totalRows += indexPath.row;
int yPosition = totalRows * cell.frame.size.height;
CGPoint offset = CGPointMake(0, yPosition);
[[table_Question[pageNumber] setContentOffset:offset animated:YES];
}
This will helps for you...
Try this code
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *aCell = [table_Question[pageNumber] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
CGSize cellSize = aCell.frame.size;
[table_Question[pageNumber] setContentOffset:CGPointMake(0, cellSize.height*2) animated:YES];
}
As per my understanding, you want to show the UITextField as well as the keyboard, when the user starts editing the UITextField inside the UITableViewCell.
If that's the case, you can set the contentInset of the UITableView to the required size and then just scroll to the required index path.
Following is a sample code:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
#try
{
table_Question[pageNumber].contentInset = UIEdgeInsetsMake(CGFloat Req_top, CGFloat Req_left, CGFloat Req_bottom, CGFloat Req_right);
[table_Question[pageNumber] scrollToRowAtIndexPath:[table_Question[pageNumber] indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//Add this to you code to scroll in the tableview gets hidden by keyboard
CGPoint scrollPoint = CGPointMake(0, Required_Height);
[yourScrollView setContentOffset:scrollPoint animated:YES];
}
}
Currently I have a UITableView with custom cells and in each cell there is a UITextField, the problem is that sometimes the UITextField is covered by the UIKeyboard.
So right now I have the Y coordinate for the UIKeyboard and my UITableView is functioning properly with the cells.
So pretty much how can I use that Y coordinate (float), in order to scroll my UITableView to that Y coordinate plus the height of the cell in order to get it right above my UIKeyboard?
Also when I the keyboard hides, how would I reset the UITableView to its normal position that it was in?
Any advice would be appreciated!
Thanks!
CustomCell *cell = (CustomCell*)[[thetextField superview] superview];
CGRect cellRect = [cell convertRect:cell.frame toView:self.view];
float bottomCell = cellRect.origin.y - cellRect.size.height;
if (bottomCell >= keyboardY) {
NSIndexPath *indexPath = [thetableView indexPathForCell:cell];
[thetableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
One or the other of UITableView's
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
I'm guessing that control isn't getting into the if statement. Consider skipping the y-coordinate check in favor of just scrolling the table view regardless of where it is.
CustomCell *cell = (CustomCell *)[[theTextField superview] superview];
NSIndexPath *indexPath = [theTableView indexPathForCell:cell];
[theTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
OK, if you're sure it's getting called, then make sure your indexPath isn't nil, which may happen if cell isn't in the tableView or if it's not a cell.
UITableView adjust itself whenever the keyboard is displayed.
However, it won't do it if you didn't set the UITableViewCellSelectionStyle of your custom cell to UITableViewCellSelectionStyleNone.
i solved by using the below code.
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];