title: UIScrollView programmatically add buttons and scroll
body: <p>I'm new to ios and Objective-C. Please be a little bit patient with me.</p>
  
  <p>THe functionality in my app, which I'm trying to develop, is a small scrolling toolbar, which contains several buttons, state toggling buttons. Also there is another button above the toolbar which slides in and out the toolbar. The second functionality I achieved successfully, using a view for the whole setup and when the slide button touch up event animates the whole view up and down.</p>
  
  <p>For the scrollview, I wanted to add buttons programmatically, hence I drew an outlet to the class and tried adding a self created button as a subview. However I cannot see the button. Nor can I observe the scrollview sliding.</p>
  
  <p>THe following is the way I'm adding the button:
  Also, my total view for the above functionality is very less in size than the total view controller</p>
  
  <pre><code>UIButton *button = [[UIButton alloc] init];
  
  button.titleLabel.text = @"hello";
  
  [_scrollTop setContentSize:self.view.frame.size];
  
  [_scrollTop addSubview:button];
  </code></pre>
  
  <p>The two images down, I hope, should help you understand the functionality I'm trying to develop. If anybody is aware of existence of similar functionality, please do direct me.</p>
  
  <p><img src="https://i.stack.imgur.com/JI5Qn.png" alt="Closed"></p>
  
  <p><img src="https://i.stack.imgur.com/0htsu.png" alt="Opened"></p>
  
  <p><strong>Edit: Code totally is</strong> </p>
  
  <pre><code>    //
  //  HCILocationViewController.m
  //  app2
  //
  //  Created by Ravi Vooda on 13/03/13.
  //  Copyright (c) 2013 housing.co.in. All rights reserved.
  //
  
  #import "HCILocationViewController.h"
  #import "HCIAnnotationViewController.h"
  
  @interface HCILocationViewController ()
  
  @property int widMap;
  @property BOOL showExploreOptions;
  
  
  @end
  
  @implementation HCILocationViewController
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view.
      _mapLocationView.delegate = self;
      MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc]
                                       initwithHouse:[self singleHome]];
      [_mapLocationView addAnnotation:annotation];
      _widMap = 10000;
  
      _showExploreOptions = NO;
  
      /*
       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button addTarget:self
                 action:@selector(aMethod:)
       forControlEvents:UIControlEventTouchDown];
      [button setTitle:@"Show View" forState:UIControlStateNormal];
      button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);//give values whatever you want.
      [_scrollTop addSubview:button];
      */
      [self addButtonsToScrollView];
  
  }
  
  - (void)addButtonsToScrollView
  {
      NSInteger buttonCount = 4;
  
      CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);
  
      for (int index = 0; index &lt;buttonCount; index++) {
          UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame];
          [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
          [button setTag:index+1];
  
          NSString *title = [NSString stringWithFormat:@"Button %d",index+1];
          [button setTitle:title forState:UIControlStateNormal];
  
          [self.scrollTop addSubview:button];
          buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
      }
  
      CGSize contentSize = self.scrollTop.frame.size;
      contentSize.height = buttonFrame.origin.y;
      [self.scrollTop setContentSize:contentSize];
  
  }
  
  - (void)buttonPressed:(UIButton *)button
  {
      NSLog(@"button pressed");
  
      switch (button.tag) {
          case 1:
              //Do something
              break;
  
          default:
              break;
      }
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  -(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id&lt;MKAnnotation&gt;)annotation {
      NSString *identifier = @"currDetailsIdentifier";
      MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapLocationView dequeueReusableAnnotationViewWithIdentifier:identifier];
  
      if (annotationView == nil) {
          annotationView = [[MKPinAnnotationView alloc]
                            initWithAnnotation:annotation
                            reuseIdentifier:identifier];
      } else {
          annotationView.annotation = annotation;
      }
  
      annotationView.enabled = YES;
      annotationView.canShowCallout = YES;
      return annotationView;
  }
  
  -(void) mapViewDidFinishLoadingMap:(MKMapView *)mapView
  {
  
      MKMapRect zoomRect = MKMapRectNull;
      for (id &lt;MKAnnotation&gt; annotation in mapView.annotations) {
          MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
          MKMapRect pointRect = MKMapRectMake(
                                              annotationPoint.x - (_widMap / 2),
                                              annotationPoint.y - (_widMap/2),
                                              _widMap,
                                              _widMap);
          if (MKMapRectIsNull(zoomRect)) {
              zoomRect = pointRect;
          } else {
              zoomRect = MKMapRectUnion(zoomRect, pointRect);
          }
      }
      [mapView setVisibleMapRect:zoomRect animated:YES];
  }
  
  -(void) setMyHouse:(NSDictionary *)singleHouse {
      self.singleHome = singleHouse;
  }
  
  
  - (IBAction)toggleExploreOptionsView:(UIButton *)sender {
  
      sender.selected = !sender.selected;
  
      if (_showExploreOptions){
          NSLog(@"Close Options");
          [UIView animateWithDuration:0.2 animations:^{
              _exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y + _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height + _exploreOptionsView.frame.size.height);
          }];
      }
      else {
          NSLog(@"Open Options");
          [UIView animateWithDuration:0.2 animations:^{
              _exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y - _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height - _exploreOptionsView.frame.size.height);
          }];
      }
      _showExploreOptions = !_showExploreOptions;
  }
  @end
  </code></pre>