title: ios: UIScrollView scrollRectToVisible not working
body: <p>I have dynamically added some buttons on the scrollView as</p>
  
  <pre><code>`int separotorWidth=2;
  
  
    [scrollView setContentSize:CGSizeMake(noOfTabs*100+(separotorWidth*(noOfTabs-1)),0)];
      UIView* tabHolder=[[UIView alloc] initWithFrame:CGRectMake(0, 0, noOfTabs*100+(separotorWidth*(noOfTabs-1)), scrollView.frame.size.height)];
  
     int x=0;
      for (int i=0; i&lt;noOfTabs; i++) {
          UIButton* tab= [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, scrollView.frame.size.height)];
          [tab setTag:i];
          x=(x+100)+separotorWidth;
          [tab setTitle:[NSString stringWithFormat:@"%@",[tabTitles objectAtIndex:i]] forState:UIControlStateNormal];
          tab.titleLabel.lineBreakMode=NSLineBreakByWordWrapping;
          tab.titleLabel.textAlignment=NSTextAlignmentCenter;
          tab.titleLabel.font=[UIFont systemFontOfSize:11];
          [tab setBackgroundColor:[ViewUtil blueColor]];
  
          [tab addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
  
          [tabHolder addSubview:tab];
          if (i==5) { //say 5th tab
              selectedBtn=tab;
          }
      }
      [scrollView addSubview:tabHolder];`
  </code></pre>
  
  <p>I want to programmatically scroll the scrollview to make sure 5th button is visible. I tried the following method but didnt work <code>[scrollView scrollRectToVisible:selectedBtn.bounds animated:YES];</code></p>
  
  <p>Kindly provide me the best way to do that.</p>
  
  <p>Edit:Answer</p>
  
  <pre><code> if ((selectedBtn.frame.origin.x+selectedBtn.frame.size.width)&gt;(scrollView.frame.origin.x+scrollView.frame.size.width)) {
  
          CGPoint btnRightTopCorner= selectedBtn.frame.origin;
          CGPoint btnleftTopCorner=btnRightTopCorner;
          btnleftTopCorner.x=btnleftTopCorner.x+selectedBtn.frame.size.width;
          CGPoint contentRightTopConer=btnleftTopCorner;
          contentRightTopConer.x=contentRightTopConer.x-scrollView.frame.size.width;
  
          scrollView.contentOffset = contentRightTopConer;
  
      }
  </code></pre>