UIPageControl Disappearing - ios

I have been working on this for quite a while now and i just cant figure it out.
My page control keeps disappearing.
I originally had my code so that it worked but it was displaying the pagecontrol in-between scrolling through views.
How ever i have changed this now and it displays when scrolling forward on two of the views and the others it doesn't work for the page control doesn't even display. When you scroll backwards to the first view the page control also disappears(including the two it was working on at the begining of running my code.
All of my views have the same settings so I'm not sure why this keeps happening.
Here is a caption of the code that I'm using:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (_pageControlUsed || _rotating) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if (self.pageControl.currentPage != page && page>=0 && page<self.childViewControllers.count) {
UIViewController *oldViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
UIViewController *newViewController = [self.childViewControllers objectAtIndex:page];
[oldViewController viewWillDisappear:YES];
[newViewController viewWillAppear:YES];
self.pageControl.currentPage = page;
[oldViewController viewDidDisappear:YES];
[newViewController viewDidAppear:YES];
_page = page;
CGRect frame = pageControl.frame;
frame.origin.x = pageWidth;
pageControl.frame = frame;
}
}
If any one knows where im going wrong and could help me out that would be brilliant.
Please be aware I'm still fairly new to programming and xcode.

Please add page control in this simple way PageControl Sample
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
and to change
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}

Related

Scrolling to specific page using pageControl and ScrollView ios

I know this question has been asked earlier but i am not getting any specific answer to my problem. I am using page control and scrollView to add view controllers and then showing all the pages using swipe. I need to redirect to a particular page on button click. I tried scrollView SetcontentOffet and every other possiblities.
-(void)ScrollToPage:(int)page
{
UIViewController *controller = [self.childViewControllers objectAtIndex:page];
if (controller.view.superview == nil)
{
CGRect frame = self.scrollView.frame;
if (frame.size.width == 0)
{
frame.size.width = self.view.frame.size.width;
frame.size.height = self.view.frame.size.height;
}
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView setContentOffset:CGPointMake(frame.origin.x, frame.origin.y) animated:YES];
self.pageControl.currentPage = page;
[self centerScrollViewContents];
[self.scrollView setContentOffset:CGPointMake(frame.origin.x, frame.origin.y)];
[self.scrollView scrollRectToVisible:frame animated:YES];
[self.scrollView addSubview:controller.view];
}
}
I assume that your pages are using your device width closely. Just follow the below steps.
- (void)viewWillAppear:(BOOL)animated {
self.myCurrentPage = 0;
[self setScrollViewPages];
self.scrollView.delegate = self;
}
- (void)setScrollViewPages {
for (int i = 0; i < numberOfPages; i++) {
UIView *myPageView = [[UIView alloc]initWithFrame:(CGRect) {self.view.frame.size.width * i
+ 10.0f,10.0f,self.view.frame.size.width - 20.0f , self.scrollView.frame.size.height-20.0f}];
myPageView.backgroundColor = [UIColor greenColor];
[self.scrollView addSubview:myPageView];
}
}
On Button Clicked
- (IBAction)buttonClicked:(id)sender {
//Pass the requried page, by default I'm passing 4
self.myCurrentPage = 4;
[self updateMyScrollView];
}
And the respective scroll view setting is
- (void)updateMyScrollView {
[self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width *self.myCurrentPage, 0) animated:YES];
self.pageControl.currentPage = self.myCurrentPage;
}
Additionally, your scrollview delegate would be like this...
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
int pageWidth = self.view.frame.size.width ;
int pageX = self.myCurrentPage*pageWidth - scrollView.contentInset.left;
if (targetContentOffset->x<pageX) {
if (self.myCurrentPage>0) {
self.myCurrentPage--;
}
}
else if(targetContentOffset->x>pageX){
if (self.myCurrentPage < numerOfPages) {
self.myCurrentPage++;
}
}
targetContentOffset->x = self.myCurrentPage*pageWidth-scrollView.contentInset.left;
DLog(#"%ld %d", (long)self.myCurrentPage, (int)targetContentOffset->x);
self.pageControl.currentPage = self.myCurrentPage;
}
Hope this helps....

The buttons / labels added to the UIScrollView are appeared only on the first page

I have a UIScrollView paging option enabled with the next code:
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 0; i < 6; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[scrollView scrollRectToVisible:frame animated:YES];
UIView *subview = [[UIView alloc] initWithFrame:frame];
itemName.text = #"Samara";
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 6, 1.0);
}
I have a number of elements (buttons, labels), added in storyboard to this ScrollView element, but in UI they're appeared only on the first page - the next pages are blank. How can I solve this?
Just reading through your code example - here is how to make your scrollview page, what you add inside those scrollview is up to you:
//Set your scrollView frame
[self.scrollView setFrame:CGRectMake(0,0,320,460];
//Will use this value to set the content width after the loop
CGFloat scrollWidth = 0;
CGFloat pageWidth = self.scrollView.frame.size.width;
CGFloat pageHeight = self.scrollView.frame.size.height;
for (int i = 0; i < 6; ++i)
{
//Create the page and make the origin.x i * pageWidth
UIView *page = [[UIView alloc] initWithFrame:CGRectMake((i * pageWidth),0,pageWidth,pageHeight)];
scrollWidth += page.width;
//Add the page
[self.scrollView addSubview:page];
}
//set the content size to the right edge of the last page (scrollWidth)
[self.scrollView setContentSize:CGSizeMake(scrollWidth,pageHeight)];
//Scroll to the last page - assuming you wanted this by looking at the code in your loop
[self.scrollView scrollRectToVisible:CGRectMake((scrollWidth - pageWidth),0,pageWidth,pageHeight)];
//Enable paging and scrolling (scrollEnabled should be YES by default)
self.scrollView.pagingEnabled = YES;
self.scrollView.scrollEnabled = YES;
Best I can do based on your code - hope it helps

content jumps on zooming out with UIScrollView

I want help with my UIScrollView sample.
I created a simple program that scrolls and zooms the content (UIImageView). It works fine, except that the content frequently disappears to the right-bottom when I try zooming out. But since I set minimumZoomScale to 1.0f, it is actually not zooming out, only the content is jumping out of the view. And what is even more weird is that I cannot scroll up after this. Apparently content size is messed up as well.
The setup I have in my sample code is as in the figure below.
When I checked the status after (trying) zooming out, I found two wrong things.
_scrollView.contentSize is 480x360, which should not be smaller than 1000x1000
_scrollView.bounds jumped to the top somehow (i.e., _scrollView.bounds.origin.y is always 0)
To cope with the two items above, I added following code in my UIScrollViewDelegate and now it works fine.
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
if(scrollView == _scrollView && view == _contentView)
{
// Setting ivars for scrollViewDidZoom
_contentOffsetBeforeZoom = _scrollView.contentOffset;
_scrollViewBoundsBeforeZoom = _scrollView.bounds;
}
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if(scrollView == _scrollView)
{
// If you zoom out, there are cases where ScrollView content size becomes smaller than original,
// even though minimum zoom scale = 1. In that case, it will mess with the contentOffset as well.
if(_scrollView.contentSize.width < CONTENT_WIDTH || _scrollView.contentSize.height < CONTENT_HEIGHT)
{
_scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
_scrollView.contentOffset = _contentOffsetBeforeZoom;
}
// If you zoom out, there are cases where ScrollView bounds goes outsize of contentSize rectangle.
if(_scrollView.bounds.origin.x + _scrollView.bounds.size.width > _scrollView.contentSize.width ||
_scrollView.bounds.origin.y + _scrollView.bounds.size.height > _scrollView.contentSize.height)
{
_scrollView.bounds = _scrollViewBoundsBeforeZoom;
}
}
}
However, does it need to come down to this? This is a very simple sequence, and it is hard to believe that Apple requires us to put this kind of effort. So, my bet is I am missing something here...
Following is my original code. Please help me find what I am doing wrong (or missing something)!
#define CONTENT_WIDTH 1000
#define CONTENT_HEIGHT 1000
>>>> Snip >>>>
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
_scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);
_scrollView.backgroundColor = [UIColor blueColor];
_scrollView.maximumZoomScale = 8.0f;
_scrollView.minimumZoomScale = 1.0f;
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_scrollView.scrollEnabled = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
_contentView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sample.jpg"]]; // sample.jpg is 480x360
CGPoint center = (CGPoint){_scrollView.contentSize.width / 2, _scrollView.contentSize.height / 2};
_contentView.center = center;
[_scrollView addSubview:_contentView];
_scrollView.contentOffset = (CGPoint) {center.x - _scrollView.bounds.size.width / 2, center.y - _scrollView.bounds.size.height / 2};
}
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
if(scrollView == _scrollView)
{
return _contentView;
}
return nil;
}
I created a quick sample project and had the same issue you described using the code you pasted. I don't exactly know what the "proper" way to zoom is in iOS but I found this tutorial which says that you need to recenter your contentView after the scrollView has been zoomed. I would personally expect it to be automatically re-centered given that it is the view you're returning in the viewForZoomingInScrollView delegate method but apparently not.
- (void)centerScrollViewContents {
CGSize boundsSize = _scrollView.bounds.size;
CGRect contentsFrame = _contentView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
_contentView.frame = contentsFrame;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
}
The code above is not written by me but is simply copied from the tutorial. I think its pretty straightforward. Also, centring the contentView seems to be a lot more elegant then constantly changing the bounds and content size of the scrollview so give it a try.
If anyone is having an issue of bouncing when you zooming out resulting background to show, try removing bounces (Bounces Zoom) in Interface Builder.
I was able to fix this problem using the delegate answer that adjusted the rates after zoom... but then I remembered I was using auto-layout, and just adding constraints for centering horizontally and vertically (in addition to the constraints tying the image to each edge of the scroll view) solved the issue for me without using the delegate methods.
Olshansk answer in swift 5
func scrollViewDidZoom(_ scrollView: UIScrollView) {
centerScrollViewContents()
}
func centerScrollViewContents() {
let boundsSize = scrollView.bounds.size;
var contentsFrame = container.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0;
} else {
contentsFrame.origin.x = 0.0;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0;
} else {
contentsFrame.origin.y = 0.0;
}
container.frame = contentsFrame;
}

UISegmentedControl setting UITableView, page control, scrollview, image view

i have a segmented control with 4 segment
segment 1 - i need a page control to swipe through photos <- -> left & right. 1- 4 photos max
Segment 2 & 3 - i need a table view
Segment 4 - i need it to be able to play video
i'm currently doing this
- (IBAction)infoAction:(id)sender {
NSString * selectedTitle = [info titleForSegmentAtIndex:[info selectedSegmentIndex]];
NSLog(#"Selected Title = %#",selectedTitle);//test
switch ([info selectedSegmentIndex])
{ case 0:
{
test.text = [frogInfo.imageFiles objectAtIndex:0];
test.textColor = [UIColor whiteColor];
[tempImageView setImage:[UIImage imageNamed:#"Detail1Temp.png"]];
break;
}
case 1:
{
test.text = frogInfo.description;
test.textColor = [UIColor whiteColor];
[tempImageView setImage:[UIImage imageNamed:#"Detail2Temp.png"]];
break;
}
case 2:
{
test.text = frogInfo.distribution;
test.textColor = [UIColor whiteColor];
[tempImageView setImage:[UIImage imageNamed:#"Detail3Temp.png"]];
break;
}
case 3:
{
test.text = [frogInfo.videoFiles objectAtIndex:0];
test.textColor = [UIColor whiteColor];
[tempImageView setImage:[UIImage imageNamed:#"Detail4Temp.png"]];
break;
}
}
}
picture with page control
section tableview
Based on what i will like to do, is it possible ? in this switch case function ?
can anybody show or have any link to tutorial on how to work out the page control swiping ?
thanks for reading
Des
I have done something very similar although I used a UISlider instead of segmented control - What you need is a pageable UISCrollview with 4 equal sized pages (UIViews) loaded horizontally one after another if take-up the entire width of an iPhone each page is 320 wide and the content size width of your scrollview will be 1280. Tie up the UIsegmented controls to scroll the pages programatically:
assuming a page width of 320:
-(IBAction)movepage:(id)sender {
int xloc = (segmentedController.selectedSegmentIndex * 320);
CGRect fieldrect = CGRectMake(xloc,0,320, pagesize.height);
[scrollView scrollRectToVisible:fieldrect animated:YES];
}
To load the scrollview and manage the pagecontroller:
pagectrl.numberOfPages = 4;
pagectrl.currentPage = 0;
scrollView.pagingEnabled = YES;
scrollView.contentSize=CGSizeMake(320* pagectrl.numberOfPages, 500);
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.bouncesZoom = NO;
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
search_url.delegate = self;
user.delegate = self;
password.delegate = self;
rpc_code.delegate = self;
// add pages
int page = 0;
CGRect frame = scrollView.frame;
pagesize = frame.size.width;
frame.origin.y = 0;
frame.origin.x = frame.size.width * page;
firstView.frame = frame;
[scrollView addSubview:firstView];
page ++;
frame.origin.x = frame.size.width * page;
locsubView.frame = frame;
[scrollView addSubview:locsubView];
page ++;
frame.origin.x = frame.size.width * page;
QRgensubView.frame = frame;
[scrollView addSubview:QRgensubView];
page ++;
frame.origin.x = frame.size.width * page;
scansubView.frame = frame;
[scrollView addSubview:scansubView];
page ++;
frame.origin.x = frame.size.width * page;
symbologysubView.frame = frame;
[scrollView addSubview:symbologysubView];
[self registerForKeyboardNotifications];
CGRect fieldrect = CGRectMake(0,0,320, pagesize);
[scrollView scrollRectToVisible:fieldrect animated:YES]; //goto 1st page
}
Note you may need to control or manage the user ability to scroll the scroll view - you do that by setting up the scrollview delegate - the code below is not fitted to your exact requirement but I'm sure you can figure out the rest!
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
// if (pageControlUsed)
// {
// do nothing - the scroll was initiated from the page control, not the user dragging
// return;
// }
// Switch the indicator when more than 50% of the previous/next page is visible
int page = floor((scrollView.contentOffset.x - pagesize / 2) / pagesize) + 1;
pagectrl.currentPage = page;
// A possible optimization would be to unload the views+controllers which are no longer visible
}

How to update UIPageControl when UIScrollView with PagingEnabled changes page?

How do i make it so that when the user scrolls to a new page in UIScrollView, the UIPageControl updates?
I have used this before and it seemed to work well. Be sure to set the UIScrollView.delegate to self.
#pragma mark - UIScrollViewDelegate methods
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
uint page = sender.contentOffset.x / width;
[self.pageControl setCurrentPage:page];
}
Where you defined your UIScrollView and UIPageControl, you have to implement this method :
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage)
return;
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
and this one :
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
where pageControlIsChangingPage is :
BOOL pageControlIsChangingPage;
Here is an update of this answer for Swift as well:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
pageControl.currentPage = Int(myScrollView.contentOffset.x) / Int(myScrollView.frame.width)
}

Resources