Incorrect Page size in UIScrollview Paging - ios

I am trying to make a simple Paging with UIScrollview, But pages are getting out after the second screen. Here is my code.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *colors =[NSArray arrayWithObjects:#"1. This is my first Page ",#"2. This is my Second Page",#"3. This is my third Page",#"4. This is my fourth Page",#"5. This is my fifth Page",nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = (scroll_events.frame.size.width) * i;
frame.origin.y = 0;
frame.size = scroll_events.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
[self.scroll_events addSubview:subview];
UITextView *myTextView = [[UITextView alloc] init];
myTextView.text = colors[i];
[subview addSubview:myTextView];
[myTextView setBackgroundColor: [UIColor redColor]];
[myTextView setTextColor: [UIColor blackColor]];
[myTextView setFont: [UIFont fontWithName:#"Lobster 1.4" size:30]];
myTextView.frame = CGRectMake(0.0,0.0,scroll_events.bounds.size.width,scroll_events.bounds.size.height);
myTextView.editable = NO;
myTextView.selectable = NO;
myTextView.scrollEnabled = NO;
}
self.scroll_events.contentSize = CGSizeMake(self.scroll_events.frame.size.width * colors.count, self.scroll_events.frame.size.height);
}
Here is the screen shot of my Scroll View

In ViewDidLoad maybe the frame of your scroll view was still not set.
Try that code in ViewDidAppear and check if it works.
Advice: You should subclass that UISCrollView and initialize the view there, not in the ViewController.
Let me know it that worked!
Regards.

Related

UIScrollView setContentOffset will add a subview to the scrollview?

I happened to find that setContentOffset to a UIScrollView will cause the scrollView append a new view to its subviews. here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((320 - 261) / 2, 50, 261, 67)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.backgroundColor = [UIColor darkGrayColor];
for (int i = 0; i < 5; i ++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 87, 1, 87, 65)];
view.tag = i;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 30)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:#"%d", i];
[view addSubview:label];
view.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view];
}
scrollView.contentSize = CGSizeMake(87 * 5, 67);
NSLog(#"before scroll: count of subview is %d", scrollView.subviews.count);
CGPoint offset = CGPointMake(87, 0);
[scrollView setContentOffset:offset animated:YES];
NSLog(#"after scroll: count of subview is %d", scrollView.subviews.count);
[self.view addSubview:scrollView];
}
Before calling setContentOffset:offset, the number of subviews of the scrollView is 5. This is what I expect. After that, the number turns to be 6.
Is it working as designed? How to avoid the new subview appended?
The extra subview is the scroll indicator. If you check again in a later method, it will have gone away. Don't worry.
You can confirm this by hiding the scroll indicators (showsVerticalScrollIndicator and showsHorizontalScrollIndicator properties).
Don't try to assume things about the view hierarchy of UIKit classes. UIKit can, and does, add its own views to several things - see tableviews and their cells, a navigation controller's view and so on.

iOS 7 - UIScrollView subView not showing

Trying to add subviews to UIScrollView, which is defined in XIB file. The code below is how I tried to add subviews in my view controller. But no subviews were added.
Any suggestions?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
CGRect frame = _tasksScrollView.frame;
frame.size.width /= 3.0;
NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor],
[UIColor redColor],
[UIColor yellowColor],
[UIColor greenColor],
[UIColor purpleColor],
nil];
for (int i = 0; i < [colors count]; i++, frame.origin.x += frame.size.width) {
UIView *subView = [[UIView alloc] initWithFrame:frame];
subView.backgroundColor = [colors objectAtIndex:i];
[_tasksScrollView addSubview:subView];
}
_tasksScrollView.contentSize = CGSizeMake(frame.size.width * [colors count], frame.size.height);
}
It may be that your subviews are offset relative to the view space in your scroll view since youre carrying over the x and y coordinates from the scrollview.frame itself which is actually a position relative to the full screen. Try setting the x and y position of the subview so that it starts from 0.
There are a few reasons why this could happen, most of the time, you just fail to link/connect the IBOutlet property (_tasksScrollView) in the XIB.

ScrollView, Views not showing up

The scroll view shows up, without any of the views visible. The view moves fine, the page control works. When i change the scroller background that all works fine. The views "the different colours" don't show. Have no idea whats wrong. Thanks for the help
-(void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 215, WIDTH_OF_IPHONE, HEIGHT_OF_SCROLLER);
int numOfPhotos = 6;
scrollViewer = [[UIScrollView alloc] initWithFrame:frame];
scrollViewer.contentSize = CGSizeMake((WIDTH_OF_IPHONE*numOfPhotos), HEIGHT_OF_SCROLLER);
scrollViewer.pagingEnabled = YES;
//scrollViewer.backgroundColor = [UIColor lightGrayColor];
[scrollViewer setDelegate:self];
int i = 0;
while (i < numOfPhotos){
CGRect frameOfView = CGRectMake((i*320), 215, 320, HEIGHT_OF_SCROLLER);
UIView *photoFrame = [[UIView alloc] initWithFrame:frameOfView];
if (i == 0){
photoFrame.backgroundColor = [UIColor redColor];//colorWithPatternImage:image1];
NSLog(#"colour was selected");
}else if (i==1){
photoFrame.backgroundColor = [UIColor blueColor];//colorWithPatternImage:image2];
}else if (i==2){
photoFrame.backgroundColor = [UIColor purpleColor];//colorWithPatternImage:image3];
}else if (i==3){
photoFrame.backgroundColor = [UIColor orangeColor];//colorWithPatternImage:image4];
}else if (i==4){
photoFrame.backgroundColor = [UIColor magentaColor];//colorWithPatternImage:image5];
}else{
photoFrame.backgroundColor = [UIColor greenColor];//colorWithPatternImage:image6];
}
[scrollViewer addSubview:photoFrame];
//[photoFrame release];
photoFrame = NULL;
i++;
}
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH_OF_IPHONE, 15)];
pageControl.center = CGPointMake(scrollViewer.center.x, (208+HEIGHT_OF_SCROLLER));
[pageControl setNumberOfPages:numOfPhotos];
[pageControl addTarget:self action:#selector(pageSwiped:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:scrollViewer];
[self.view addSubview:pageControl];
My .H file looks like this
#interface HomePage : UIViewController <UIActionSheetDelegate, UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollViewer;
UIPageControl *pageControl;
BOOL pageControlUsed;
}
#end
Try setting the y origin of the photoFrame to 0 as it will place it under the actual frame of your scrollview.
If not the problem you are having is that you are making the photoFrame NULL. If you are using ARC you shouldn't worry as the Garbage Collector will take care of it.

Xcode - subview not visible

I have created a UIViewController with a UIScrollView. On the scrollView I have a page control. When I was testing it I was able to scroll horizontally through four different color pages. Now I need to add the actual views I need. In a different part of the project I created a line graph using coreplot. Now I have added made a new UIview and added the code for the line graph. I am now trying to replace the first page of the scrollView with the linegraph UIView. I am getting no errors however nothing is appearing on the screen either. It is just the default background of the scrollView. The scrollView still scrolls through the other pages correctly. I added a button programatically to the UIView to see if it had something to do with coreplot but I cannot see that either. Here is some of my code. I would appreciate any advice. Thanks.
The viewDidLoad method for my main viewController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor], nil];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
self.alertsSubView = [[AlertsView alloc] initWithFrame:frame];
[self.scrollView addSubview:_alertsSubView];
for (int i = 1; i < numberOfGraphs; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
self.alertsSubView.delegate = self;
_scrollView.delegate = (id)self;
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
Here is the code for the initWithFrame method from my UIView. All of the NSLog statements get run so I know everything is being called:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"AlertsView initWithFrame");
///button for testing
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonFrame = CGRectMake(0,0, frame.size.width, frame.size.height);
self.button.frame = buttonFrame;
[self.button addTarget:self
action:#selector(buttonTouched)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.button];
[self initPlot];
}
return self;
}
I am also adding the methods initPlot and ConfigureHost in case they are relevant:
-(void)initPlot {
NSLog(#"into init plot");
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
NSLog(#"endo of init plot");
}
-(void)configureHost {
NSLog(#"into configure host");
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.bounds];
self.hostView.allowPinchScaling = YES;
[self addSubview:self.hostView];
}
I finally figured this out. Thanks for your suggestion #Eric Skroch. It actually led me down the path to the right answer even though it was not the problem. Basically to fix this problem I had to move all of the logic into the view controller and change the order of alot of the code. I am posting the relevant parts of my revised view controller here in case anyone runs across anything similar.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_alertsSubView initPlot];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor], nil];
alertFrame.origin.x = self.scrollView.frame.size.width;
alertFrame.origin.y = 0;
alertFrame.size = self.scrollView.frame.size;
_panel = [[UIView alloc]initWithFrame:alertFrame];
for (int i = 1; i < numberOfGraphs; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
_scrollView.delegate = (id)self;
self.alertsSubView.delegate = self;
}
-(AlertsView *)alertsSubView
{
if(!_alertsSubView)
{
CGRect alertsViewFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
_alertsSubView = [[AlertsView alloc] initWithFrame:alertsViewFrame];
_alertsSubView.backgroundColor = [UIColor purpleColor];
[self.scrollView addSubview:self.alertsSubView];
}
return _alertsSubView;
}

Change text on swipe

I am creating a text jokes app what i need is i want the UITextView in swipe form like when i swipe the first joke after a gap another UITextview comes in with another joke , so far what i have done is i have manually created the UITextView ( like if there is 10 jokes then 10 uitextview ) and added them in a scroll but that is taking too much time in viewdidload method and the app takes 6,7 second to load ...
if you have anyother idea please do let me know
for (int i = 0; i < arrresult.count; i++) {
CGRect frame;
frame.origin.x = self.scrollmain.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollmain.frame.size;
// UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
// UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BG_text.png"]];
subview2.backgroundColor = [UIColor whiteColor];
[subview setFont:[UIFont boldSystemFontOfSize:14]];
[subview2 setFont:[UIFont boldSystemFontOfSize:14]];
subview.layer.cornerRadius = 10 ;
subview2.layer.cornerRadius = 10;
NSString *p = [duaobj2 dua_arabic];
NSString *b = [duaobj2 eng_trans];
subview.text = p;
subview2.text = b;
[self.scrollmain addSubview:subview];
[self.scrollmain addSubview:subview2];
subview.editable = NO;
subview2.editable = NO;
subview.textColor = [UIColor blackColor];
subview2.textColor = [UIColor blackColor];
subview.backgroundColor = [UIColor whiteColor];
subview2.backgroundColor = [UIColor whiteColor];
scrollmain.showsHorizontalScrollIndicator = NO;
CGRect frame1 = subview.frame;
CGRect frame2 = subview2.frame;
frame1.size.height = subview.contentSize.height;
frame2.size.height = subview2.contentSize.height;
subview.frame = frame1;
subview2.frame = frame2;
}
self.scrollmain.contentSize = CGSizeMake(self.scrollmain.frame.size.width * arrresult.count,self.scrollmain.frame.size.height);
scrollmain.pagingEnabled = TRUE;
scrollmain.delegate = self;
There is no need to load the subviews at the time of viewDidLoad.
You should delay this by using scrollView delegate methods.
You should create only 3 textviews at max. Current, previous and next.
On scrollViewDidScroll delegate method, you should wisely remove previously loaded textview and you should add new textview.

Resources