I'm new to iOS. I want to disable scrollovertop in UITableView. I did try [self.table setBounce:NO], but my table's scrolloverbottom was disabled as well. I just want to disable scrollovertop. Please help me out.
Try this
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point = [scrollView contentOffset];
if (point.y < 0)
{
[scrollView setContentOffset:CGPointMake(0, 0)];
}
}
Don't forget to add UITableViewDelegate
Related
I have a tableview in which UITableViewCell contains a textfield. When user tap on the textfield and keyboard appears, I scroll to the tapped in row after setting proper content inset.
Now, I have a UIView shown on top of my table view which I need to adjust accordingly when table scrolls.
To do this, I implemented scrollViewDidScroll: method as below. While this works in certain situations, it do not work or give random results in some situations. Am I doing something wrong here?
- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.hollowView) {
CGPoint offset = iScrollView.contentOffset;
CGRect hollowViewFrame = self.hollowView.hollowFrame;
hollowViewFrame.origin.y += self.previousOffset - offset.y;
self.previousOffset = offset.y;
[self.hollowView resetToFrame:hollowViewFrame];
}
}
- (void)keyboardDidHide:(NSNotification *)iNotification {
self.previousOffset = 0.0;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
If you want to scroll the both view simultaneously then you should have to take both views with same size and in following method just set the scrollview content offset to them. It will surly work
enter code here - (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.scrollView) // Check for the scrolled view
{
// Set the Tableview offset directly
tableview.contentOffset = iScrollView.contentOffset
}
else {
//Set view scroll as per tableview scroll
view.contentOffset = iScrollView.contentOffset } }
//Note :- Make sure view should be scrollview type or if view is getting scared or shows black screen
Thanks..
Use this library:
https://github.com/hackiftekhar/IQKeyboardManager
It will handle the position of Views when tapped on a textfield.
So, this piece worked for me for anyone who is struggling with same situation:
- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.hollowView) {
CGRect frame = [self.tableView rectForRowAtIndexPath:self.expandedCellIndexPath];
frame.origin.y -= self.tableView.contentOffset.y;
[self.hollowView resetToFrame:frame];
}
}
you can use this for scrolling table in ios.
-(void )keyboardWillShow:(NSNotification *)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin =[keyboardInfovalueForKey:UIKeyboardFrameEndUserInfoKey];
keyBoardHeight = keyboardFrameBegin.CGRectValue.size.height;
vwSendBottomSpaceContraint.constant = keyBoardHeight
[self performSelector:#selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1];
}
-(void)keyboardWillHide:(NSNotification*)notification
{
vwSendBottomSpaceContraint.constant = 0;
[self performSelector:#selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1];
}
-(void)scrollToBottomAnimated
{
NSInteger rows = [tableview numberOfRowsInSection:0];
if(rows > 0)
{
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottomanimated:YES];
}
}
I want to add three views in a UIViewController,
UIImageView
Scrollable Segment control (HMSegmentedControl)
UIViews (or) UITableviewCells
it looks like this,
When the whole view scrolls the segment control should pin at the top of the screen (like header in UITableview).
My code
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ((long)scrollView.tag == 10) {
// Main scrollview
if (self.lastContentOffset >= scrollview.contentOffset.y) {
NSLog(#"Down");
if (scrollview.contentOffset.y < 158.0f) {
[scrollview setContentOffset:CGPointMake(scrollview.contentOffset.x, scrollview.contentOffset.y)];
[matchInfoTable setScrollEnabled:NO];
[scrollview setScrollEnabled:YES];
}
} else if (self.lastContentOffset <= scrollview.contentOffset.y) {
NSLog(#"Up");
NSLog(#"%f",scrollview.contentOffset.y);
if (scrollview.contentOffset.y > 138.0f) {
[scrollview setScrollEnabled:NO];
[matchInfoTable setScrollEnabled:YES];
}
if (scrollview.contentOffset.y >= 163.0f) {
[scrollview setContentOffset:CGPointMake(scrollview.contentOffset.x, 163.0f)];
[scrollview setScrollEnabled:NO];
[matchInfoTable setScrollEnabled:YES];
}
}
self.lastContentOffset = scrollview.contentOffset.y;
NSLog(#"LastOffset :: %f",self.lastContentOffset);
} else if (scrollView.tag == MATCH_INFO) {
// match info table
if (scrollView.contentOffset.y == 0) { // TOP
[scrollview setContentOffset:CGPointMake(scrollview.contentOffset.x, scrollview.contentOffset.y)];
[matchInfoTable setScrollEnabled:NO];
[scrollview setScrollEnabled:YES];
}
}
}
In this code when the segment control pin at the top. The below view is not scrolling continuously. I need to trigger its scrolling again.
All answers are appreciated!!
I think better you should use UIPageViewController and put inside all tableviews, then you can detect swipes and change tabs.
Also you can use UISwipeGestureRecognizer and detect left and right swipes described in this post left and right swipes
I'm trying to implement a growing UITextView with the content size, I am using this class and autolayout and it works great but my problem is that my inner scroll view is not scrolling.
It was scrolling before adding the methods for growing text view in viewDidLoad.
This is what is in my xib:
View
outerScrollView
innerScrollView
labels
Growing UITextView
I do this to switch scrolling between scrollview
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if([scrollView isEqual:self.innerScrollview]){
self.outerScrollview.scrollEnabled = NO;
self.innerScrollview.scrollEnabled = YES;
}
else{
self.outerScrollview.scrollEnabled = YES;
self.innerScrollview.scrollEnabled = NO;
}
}
and in viewDidLoad where the "msg" is the growing textview:
self.msgHandler = [[GrowingTextViewHandler alloc]initWithTextView:self.msg withHeightConstraint:self.msgHeight];
[self.msgHandler updateMinimumNumberOfLines:1 andMaximumNumberOfLine:INT_MAX];
and in textViewDidChange
- (void)textViewDidChange:(UITextView *)textView {
[self.msgHandler resizeTextViewWithAnimation:YES];
}
so why is the growing text affecting the scrolling of the inner scrollview?
int outerContentHeight = self.outer_last_label.frame.size.height + self.outer_last_label.frame.origin.y;
int innerContentHeight = self.inner_last_label.frame.size.height + self.inner_last_label.frame.origin.y;
[self.outerSrollview setContentSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width,outerContentHeight)];
[self.innerScrollview setContentSize:CGSizeMake(self.scrollview2.frame.size.width, innerContentHeight)];
I have managed to fix it but I removed the use of the class"GrowingTextViewHandler" and I did this instead
I changed "scrollViewDidScroll" to this:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if([scrollView isEqual:self.innerScrollview]){
[self.view insertSubview:self.innerScrollview atIndex:0];
}
else{
[self.view insertSubview:self.outerScrollview atIndex:0];
}
}
and changed "textViewDidChange" to this
- (void)textViewDidChange:(UITextView *)textView {
float height = self.msg.contentSize.height;
[UITextView beginAnimations:nil context:nil];
[UITextView setAnimationDuration:0.5];
self.msgHeightConstraint.constant = height + 10.0; //Give it some padding
[UITextView commitAnimations];
}
Please set the content size of the scroll view, Do not change anything in the code, and check,
[self.scrollViewOuter setContentSize:CGSizeMake(200, 1000)];
[self.scrollViewInner setContentSize:CGSizeMake(200, 1000)];
My both the scroll views are working if, I add content size in it.
When I add some hidden header view, like search bar, to the table view, it scrolls automatically to content's top or table cell's top, when I set the offset of the scroll to the middle of a search bar. (I used below code)
// in viewDidLoad section
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]
[self.tableView setTableHeaderView:searchBar]
// in viewWillAppear: section
[self.tableView setContentOffset:CGPointMake(0, 44)];
For example, if I scroll up a little amount when the search bar is hidden, it automatically scrolls to display the entire search bar.
And if I scroll down a little when the search bar is displayed, it automatically scrolls to make the search bar hidden.
I used below code to implement this feature to my collection view's header, but that wasn't exactly the same as the table view's feature.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGFloat offset = scrollView.contentOffset.y;
if (offset > 22 && offset < 44) {
[scrollView setContentOffset:CGPointMake(0, 44) animated:YES];
} else if (offset > 44) {
return;
} else {
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
I think it is very hard to mimic above feature exactly, because the judgment of displaying the entire search bar or hiding the search bar is very subtle.
So my question is, "Is there any pre-implemented method in iOS SDK, or in UICollectionViewController?".
I googled for many hours but I couldn't found the answer.
I couldn't find the pre-implemented way to implement that feature, but the below way seems to make things similar to that of table view's.
First, add CGFloat type ivar originScrollOffset.
Then, I used the below code to implement scroll view delegate.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
originScrollOffset = scrollView.contentOffset.y;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGFloat endScrollOffset = scrollView.contentOffset.y;
NSLog(#"%f %f", originScrollOffset, endScrollOffset);
if (endScrollOffset <= 44 && endScrollOffset >= 0) {
if (originScrollOffset >= 44) {
if (originScrollOffset - endScrollOffset > 10) {
originScrollOffset = 0;
[self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
} else {
originScrollOffset = 44;
[self.collectionView setContentOffset:CGPointMake(0, 44) animated:YES];
}
} else {
if (endScrollOffset - originScrollOffset > 10) {
originScrollOffset = 44;
[self.collectionView setContentOffset:CGPointMake(0, 44) animated:YES];
} else {
originScrollOffset = 0;
[self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
}
}
i'm stuck on a problem with UIScrollView
i wanna the scroll view be zoomed when the user tap on a specific point, and prevent the user to zoom in/out using Pinch Zoom Gestures, how can i do it?
[imgScroll setDelegate:self];
float minimumScale = [imgScroll frame].size.height / [imageView frame].size.height;
[imgScroll setMinimumZoomScale:minimumScale];
[imgScroll setZoomScale:minimumScale];
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [imageView viewWithTag:ZOOM_VIEW_TAG];}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];}
thx in advance.
I was disable the zoom propriety in the `UIScrollView, checked all the propriety under the touch section in Interface Builder.