I have an UIScrollview(Horizontal) with a UIview as a subview, here i can able to drag the uiview inside the scrollview, the contentSize of scrollview is 2400. When i dragging that uiview it's not visible i.e, it's going inside the scrollview because my scrollview width is small, i need to scroll the scrollview to make visible of UIView. For this i have use
[myscroll scrollRectToVisible:myview.frame animated:YES];
but still its not working, please help me out.
In Your case,[myscroll scrollRectToVisible:myview.frame animated:YES]; will not work because of myview is a sub-view of myscroll. myview.frame will return the CGRect which is only related to the myscroll.
My suggestion is , you can acheive the functionality through UIpageControl + UIScrollView . You can set UIPageControl as Hidden.
SampleCode
int page = sidePager.currentPage + 1;
CGRect frame = scroller.frame;
frame.origin.x = frame.size.width * page;
if (0 != UpAndDownPager.currentPage) {
frame.origin.y = frame.size.height * (UpAndDownPager.currentPage + 1 );
}
scroller scrollRectToVisible:frame animated:YES];
sidePager.currentPage = sidePager.currentPage + 1;
Note:
sidePager: UIPageControl For Right-Left position
UpAndDownPager: UIPageControl For Top-Bottom position
scrollView.contentSize.width != 0
&&
scrollView.contentSize.height != 0
Related
I have UIScrollView on which I am using 2 UIButtons. UIScrollView initial frame is (0,0,768,1024). I want when I click on first button the UIScrollView y-cordinate position will be 500, means it will scrolldown 500 positions below to its actual position.
And when I click second Button which Will show on UIScrollView, Position Y-cordinate of UIScrollView y=535, then UIScrollView Move up to Position y= 0 again.
First button click UIScrollView y = 500
Second button click UIScrollView y = 0
Any hints from experts would be very welcome.
Maybe you do not want to change the frame of the scroll view (this would just displace the whole view), but the contentOffset. This can be animated, too.
CGPoint offset = scrollView.contentOffset;
offset.y += 500;
[scrollView setContentOffset:offset animated:YES];
This may solve ur pbm
-(IBAction) clickfirstbutton
{
[scrollview setContentOffset:CGPointMake(0, 500)) animated:TRUE];
}
-(IBAction) clicksecondbutton
{
[scrollview setContentOffset:CGPointMake(0, 0) animated:TRUE];
}
If u don't want animations use
[scrollview setContentOffset:CGPointMake(0, 0) animated:FALSE];
I wanted to lock search bar on top of table view when y boundary reaches 0 ( or beyond - value)
I tried to in scroll view delegate method, but nothing really changed. In fact, the search bar's frame changed, but it still behaved as default.
Any ideas?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UISearchBar *searchBar = self.searchDisplayController.searchBar;
CGRect rect = searchBar.frame;
rect.origin.y = MAX(0, scrollView.contentOffset.y);
self.searchDisplayController.searchBar.frame = CGRectMake(0,MAX(0,scrollView.contentOffset.y),320,44);
}
In your viewdidload method you need to offset the table view:
self.tableView.contentOffset = CGPointMake(0.0, 44.0);
Then In IB drag a searchbar above your UITableview in your UINavigation controller
Or prgrammically add it.
You need to call setNeedsLayout if you're running on iOS 6.0+.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UISearchBar *searchBar = self.searchDisplayController.searchBar;
CGRect rect = searchBar.frame;
rect.origin.y = MAX(0, scrollView.contentOffset.y);
[scrollView setNeedsLayout]; // <-- Call setNeedsLayout here.
self.searchDisplayController.searchBar.frame = CGRectMake(0,MAX(0,scrollView.contentOffset.y),320,44);
}
I have a UIScrollView that I can swipe through to display new views. I have a button that I want to programmatically scroll through the views. So if I click it once, it will scroll to the next view. How can I implement this?
Here is the scrollview code
- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{
NSUInteger offset = aScrollView.contentOffset.x;
NSUInteger width = aScrollView.contentSize.width;
int anIndex = (offset == 0) ? 0 : (int)(width/(width - offset));
selectedDeal = [deals objectAtIndex:(anIndex > [deals count]- 1 ? [deals count]- 1 : anIndex)];
}
[myScrollView scrollRectToVisible:myView.frame animated:TRUE];
This will scroll so that myView becomes visible, assuming myView is added to the scroll view and it is not already visible. You need to keep track of current visible view and in the button handler you need to pass next view's frame as parameter to the above method.
[myScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0)animated:YES];
[myScrollView scrollRectToVisible:self.view.frame animated:YES];
I'm trying to do a horizontal scroll of UILabels with different widths.
I've already put all labels next to each other inside the UIScrollView, but since the page scrolling, bouncing and snapping is done in scrollview.frame.width "steps", i cannot set it to work as I'd wish.
Can this be done? Thank you so much :)
What happens if you set the size of your width property to be the width of the next label? Something like (in your scroll view delegate) :
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// Get the label that's going to slide into view
UIView *label = [self getCurrentLabel];
// Get it's width
float width = [label frame].size.width;
// Set our size
CGRect frame = [scrollView frame];
frame.size.width = width;
[scrollView setFrame:frame];
}
PS I've not tried this so it might just fail horribly!
I'm using an UIScrollView and I have an image that indicates to the user that there is more content that they can scroll through vertically. I would like this image to be hidden when the scrollview is all the way at the bottom. Is there a way to do this? Would I have to subclass UIScrollView and make my own?
your scroll view's delegate should repsond to scrollViewDidEndScrollingAnimation: and use that to check where you are
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// Get some details about the view
CGSize size = [scrollView frame].size;
CGPoint offset = [scrollView contentOffset];
CGSize contentSize = [scrollView contentSize];
// Are we at the bottom?
if (-offset.y + size.height <= contentSize.height)
NSLog(#"bottom");
else
NSLog(#"not bottom");
}
NB The if statement was done in my head so it might be the wrong way round ;)