Search code examples
iphoneobjective-cipod

Button action not working when button frame is changed


Am trying to change the button frame according to the orientation change but button action is not detecting if frame is changed.

"addButtonsOnScrollView" methode is called from "viewWillAppear" method

Here is my code

//scaleFactor = 320 if portrait orientation scaleFactor= 480 if landscape orientation

- (void) addButtonsOnScrollView 
{ 
   containerViewForButtons = [[UIView alloc]initWithFrame:CGRectMake(scaleFactor-100, 22, 100, 37)];
      addButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [addButton addTarget:self action:@selector(addFriend:)forControlEvents:UIControlEventTouchDown];
  [addButton setBackgroundImage:[UIImage imageNamed:@"add_btn.png"] forState:UIControlStateNormal];
  addButton.frame = CGRectMake(0, 0, 37, 37);   
  [containerViewForButtons addSubview:addButton];
    [self.view addSubview:containerViewForButtons];
}

- (void) addFriend:(UIButton *) button {
   NSLog("Button clicked....");
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
   if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {

       [self layoutLandscapeView];
      }
      else if (interfaceOrientation == UIInterfaceOrientationPortrait) {

       [self layoutPortraitView];
      }
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void) layoutLandscapeView {

     containerViewForButtons.frame = CGRectMake(380, 22, 100, 37);

}
- (void) layoutPortraitView {

    containerViewForButtons.frame = CGRectMake(220, 22, 100, 37);
}

Solution

  • Are you adding the button to a scrollview (looks like it from the method name)? Most likely your scrollview's content size is not getting updated when you rotate. That is why the button is not able to detect touch.