ScrollView, Views not showing up - ios

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.

Related

How to add UIActivityIndicatorView to the bottom of UITableView, and toggle it on loadings

I have a "scroll to load more" behavior on my UITableView. It starts loading more content when you scroll to the bottom. What I want to achieve is to show a UIActivityIndicatorView at the bottom of the UITableView when service call is in progress, and hide it when new items are added.
What is a good way to solve this problem? I am using Swift but objective-C solutions are also welcome.
I have implemented the same functionality in objective-C
In the cellForRowAtIndexPath
if (indexPath.row == [self.yourArrayData count] - 1)
{
// Call the API
[self loadMoreData:#"all" andPages:[NSString stringWithFormat:#"%ld", (long)pages] AndIsLoadMore:YES];
}
Add loader or design
-(void)addLoaderViewAtBottom {
viewLoader = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 50)];
[viewLoader setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:viewLoader];
UIActivityIndicatorView *activityIndicator1 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator1.center = CGPointMake((SCREEN_SIZE.width/2), 30);
activityIndicator1.alpha = 1.0;
activityIndicator1.hidden = NO;
[viewLoader addSubview:activityIndicator1];
[activityIndicator1 startAnimating];
}
-(void)hideLoaderView {
[UIView animateWithDuration:1
animations:^(void) {
[viewLoader setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50)];
}];
}
then use this two methods in your API call method. Call addLoaderViewAtBottom when API call starts and call hideLoaderView after getting the responses
Happy coding...
Initialize UIActivityIndicatorView:
#interface MyBulletinVC ()
{
UIActivityIndicatorView *activityLoadMore;
}
-(void)viewDidLoad
{
activityLoadMore = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
}
Check when UITableView is being scrolled and what is the content offset
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
float tblBottomEdge = _tblBulletin.frame.size.height+_tblBulletin.frame.origin.y;
if (bottomEdge <= tblBottomEdge)
{
return;
}
if (bottomEdge >= scrollView.contentSize.height)
{
[self show_bottom_loader];
}
}
If applicable as per content offset then display loader in footer of table:
-(void)show_bottom_loader
{
_tblBulletin.tableFooterView = [self returnLoaderViewWhileFetchingRecords];
CGPoint newContentOffset = CGPointMake(0, [_tblBulletin contentSize].height - _tblBulletin.bounds.size.height);
[_tblBulletin setContentOffset:newContentOffset animated:YES];
[self getNewsRecords];
}
Hide footer when data is loaded:
-(void)hide_bottom_loader
{
_tblBulletin.tableFooterView = nil;
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
[_tblBulletin setTableFooterView:v];
[activityLoadMore stopAnimating];
}
-(UIView*)returnLoaderViewWhileFetchingRecords
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,_tblBulletin.frame.size.width,44)];
view.backgroundColor =[UIColor clearColor];
UILabel *lblBg =[[UILabel alloc]initWithFrame:CGRectMake(0,1,_tblBulletin.frame.size.width,42)];
lblBg.backgroundColor =[UIColor whiteColor];
[view addSubview:lblBg];
UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(_tblBulletin.frame.size.width/2 - 20,0,_tblBulletin.frame.size.width/2,view.frame.size.height)];
lbl.textAlignment = NSTextAlignmentLeft;
lbl.text = #"Loading...";
lbl.font = [UIFont fontWithName:#"Arial" size:14.0f];
lbl.textColor = [UIColor darkGrayColor];
lbl.backgroundColor = [UIColor clearColor];
lbl.userInteractionEnabled = NO;
[view lbl];
activityLoadMore.frame = CGRectMake(_tblBulletin.frame.size.width/2-35,22,0,0);
[activityLoadMore startAnimating];
[view addSubview:activityLoadMore];
return view;
}
-(void)getNewsRecords
{
//fetch new records and add to your array.
[self hide_bottom_loader];
//reload table.
}
Replace tblBulletin with you table name.

Incorrect Page size in UIScrollview Paging

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.

Row height of the uitableview in differing gradually

I have two uitableviews in a view controller. First table is set on a uiscrollview programatically and the other table on self.view from storyboard. I have set the rowHeight for both the table as 45. But gradually the height of one table keeps increasing as the rows increase like the screenshot I have shown.
Code goes like this :
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(130, 45, self.view.frame.size.width-130, self.view.frame.size.height-45)];
scrollview.backgroundColor = [UIColor colorWithRed:0.42 green:0.27 blue:0.60 alpha:1.0];
tableGradeValue = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, self.view.frame.size.width, self.view.frame.size.height-75) style:UITableViewStylePlain];
tableGradeValue.delegate = self;
tableGradeValue.dataSource = self;
self.tableCourse.delegate = self;
self.tableCourse.dataSource = self;
tableGradeValue.rowHeight = 45;
scrollview.bounces = NO;
for(int i = 0; i< [res6Keys count]; i++)
{
CGFloat y = i * 150;
UILabel *lblCell = [[UILabel alloc] initWithFrame:CGRectMake(y, 1, 140, 31)];
lblCell.tag = i+1;
[scrollview addSubview:lblCell];
lblCell.backgroundColor = [UIColor clearColor];
lblCell.textColor = [UIColor colorWithRed:0.93 green:0.92 blue:0.96 alpha:1.0];
lblCell.text = [res6Keys objectAtIndex:i];
}
[scrollview addSubview:tableGradeValue];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scrollview];
And the other tableview is set from storyboard normally and the rowHeight is 45.
I don't know wether this was the problem. But strangely I got the answer. I dint give #property and #synthesis to the table which was over the scrollview. I now gave property and synthesis and it got working.

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;
}

UIScrollView programmatically Scrolling

I have a UITextView inside a UIScrollView that needs to automatically scroll once the user starts editing it. This is because the keyboard will cover the textview.
Here is the code -
In viewDidLoad:
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
and here's the delegate method implementation -
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[scrollView setContentOffset:point animated:YES];
}
However, nothing happens.
your are doing fine but use feedBackformView instead of scrollView,while setting content offset in textViewDidBeginEditing:method, just have a look on below code
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
[feedBackformView addSubview:commentsView];
[self.view addSubview:feedBackformView];
in textview DelegateMethod,
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[feedBackformView setContentOffset:point animated:YES];
}
hope it will hepls you...
You can use scrollView scrollRectToVisible:animated or the setContentOffset:animated methods described here:
UIScrollView Class Reference
Keep in mind that UITextView comes with its own scroll view. so you may not need to nest it inside another scrollview. But you may need to if your app is that interesting.
Is your point of scrolling already at a visible point?
-(void)textViewDidBeginEditing:(UITextView *)textView{
// try this instead
CGPoint point = CGPointMake(textView.frame.origin.x,
textView.frame.origin.y + textView.frame.size.height);
[scrollView setContentOffset:point animated:YES];
// What does this produce?
NSLog(#"%# %#", NSStringFromCGPoint(scrollView.contentOffset),
NSStringFromCGRect(textView.frame));
}

Resources