scrollRectToVisible not working in page view controller - iPhone sdks - ios

I am using Xcode 6.1 and developing for iOS 8.0.
In my application I have a UIPageViewController. In that I have added a UIScrollView. In the UIScrollView I have 4 buttons. When I tap on a button I use this code.
long btnValue = sender.tag;
float spaceing = 0;
if(btnValue == 1)
spaceing = self.scrollView.frame.size.width/4*btnValue;
else
spaceing = (self.scrollView.frame.size.width/2*btnValue) - self.scrollView.frame.size.width/4;
CGRect frame;
frame.origin.x = spaceing;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
self.scrollView.pagingEnabled = NO;
This(scrollRectToVisible:) is not working. Can anyone help me?
Thanks.

long btnValue = sender.tag;
float spaceing = 0;
if(btnValue == 1)
spaceing = self.scrollView.frame.size.width/4*btnValue;
else
spaceing = (self.scrollView.frame.size.width/2*btnValue) - self.scrollView.frame.size.width/4;
CGRect frame;
frame.origin.x = spaceing;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
self.scrollView.pagingEnabled = NO;
// Problem is here
frame.size = self.scrollView.frame.size; //scrollView.frame.size matters
set
frame.size = self.View.frame.size;
and if you are doing it by coding then please add your scrollview into your view
[self.view addSubview:yourScrollView];

Related

URL is not loading completely on my UIWebView in iOS?

I am working on an iPhone application, where i am opening an URL to my UIWebView. The problem is URL is not loading perfectly. As per my requirement, i need to add UIWebView as footer view of my table. but the problem is after getting the web view content height size from webViewDidFinishLoad method, i am setting frame of my footer view. here is my code :
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
aWebView.scrollView.scrollEnabled = NO;
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
[self setFooterView:fittingSize.height + aWebView.frame.origin.y];
}
-(void)setFooterView:(float)fittingHeight
{
bottomView.frame = CGRectMake(0, 0, 320, fittingHeight);
webViewSocial.frame = bottomView.frame;
[tableView reloadData];
}
But when i am scrolling my table view, then my UIWebView (footer view) is not scrolling completely. Can you please tell me how can i achieve my output. Thanks!
try to set like this
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = self.webView.frame;
frame.size.height = height;
frame.size.width = width;
self.webView.frame = frame;
self.tableView.tableFooterView = webView;
}

xcode 5 horizontal scrollview doesn't work (iOS7)

Im trying to create an filter bar with a horizontal scroll view.
How can I create the horizontal scrollview in xcode 5 for iOS7?
This is my code:
[self.scroll setScrollEnabled:YES];
[self.scroll setDelegate:self];
self.scroll.backgroundColor = [UIColor clearColor];
scroll.contentMode = UIViewContentModeScaleToFill;
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
[self.view addSubview:scroll];
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
For scroll view to scroll horizontally the width of the content size should be larger than the actual frame of scroll view.
If your scroll view frame size is (320, 143), the width of content size of scroll view should be larger than 320 so that the scroll view scroll.
For horizontal scroll view you can use, EasyTableView. Its simple to use.
You will need to get use to the facilities - the horizontal scrollview works and it works in different ways including simulating pages that you can flick right or left.
scrollview has a physical size (frame) and a content size, the thing with scrollviews is the content size will be larger than the physical dimensions so I can for example have a UIImageView of 930x 250 viewable in an iPhone with 320 x 480
(I'll skip the rest of the setup - just what's essential - for all the other tricks look into the apple sample projects - there is a lot - Note also that a lot of apps just use the Interface Builder to paint the view with the scrollview )
UIScrollview *scroll = [[UIScrollview alloc] init]
scroll.frame = CGSizeMake (0,100,320,250);
scroll.contentsize = CGSizeMake(930,250)
UIImageView *imageview = [[UIImageView alloc]init];
imageview.image = myimage; //(UIImage)
[self.view addSubview:scroll]; // add subview as the bottom layer to the main view
[scroll addSubview imageview];
Below is something more complex with pages:
pagectrl.numberOfPages = 7;
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];
page ++;
frame.origin.x = frame.size.width * page;
gaView.frame = frame;
[scrollView addSubview:gaView];
page ++;
frame.origin.x = frame.size.width * page;
upcsubView.frame = frame;
[scrollView addSubview:upcsubView];
[self registerForKeyboardNotifications];
if (gotopage == 7) {
int xloc = ((gotopage - 1) * pagesize);
CGRect fieldrect = CGRectMake(xloc,0,320, pagesize);
[scrollView scrollRectToVisible:fieldrect animated:YES];
}

display selected thumbnail for initial subview in a scrollview

Trying to get the selected image from an array of images in a collection view to be the initial subview in a scrollview. Right now the scrollView loads the first image from the array, no matter which image is tapped on in the collection view. Please give some advice or websites with information on how to achieve this. Thanks!
for (int i = 0; i < self.album.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [self.album objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.album.count, self.scrollView.frame.size.height);
}
At the time of creating scrollview add below line
self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.selectedImage, self.scrollView.frame.size.height);
where self.selectedImage is the index of the image in the array
PD: Sorry for my english.

UIViews are blank when added to UIScrollView

I'm trying to add the Views of a ViewControllerClass to a ScrollView.
The problem is that only the first page is showing up, the other ones are blank.
This is my code
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[imageArray2 addObject:[[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
for(WeatherViewController *iv in imageArray2)
{
[self.scrollView addSubview:iv.view];
}
Any advice?
You have to assign your calculate frame to your WeatherViewController's view on your loop.
Try this
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
WeatherViewController *myWeatherVC = [[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
myWeatherVC.view.frame = frame;
[imageArray2 addObject:myWeatherVC];
[self.scrollView addSubview:myWeatherVC.view];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
Just assign the frame like this and it will work.
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
WeatherViewController *thisView = [[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
[thisView.view setFrame:frame];
[imageArray2 addObject:thisView];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
for(WeatherViewController *iv in imageArray2)
{
[self.scrollView addSubview:iv.view];
}
Hope this will help you.

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

Resources