UIButton does not respond to touch events after changing its position using setFrame

Posted by Pranathi on Stack Overflow See other posts from Stack Overflow or by Pranathi
Published on 2011-03-29T05:31:33Z Indexed on 2012/03/22 11:29 UTC
Read the original article Hit count: 153

Filed under:
|
|
|

I have a view controller class (child) which extends from view controller class (parent). In the parent class's loadView() method I create a sub-view (named myButtonView) with two buttons (buttons are horizontally laid out in the subview) and add it to the main view. In the subclass I need to shift these two buttons up by 50pixels.

So, I am shifting the buttonView by calling the setFrame method. This makes the buttons shift and render properly but they do not respond to touch events after this. Buttons work properly in the views of Parent class type. In the child class type view also, if I comment out the setFrame() call the buttons work properly.

How can I shift the buttons and still make them respond to touch events?

Any help is appreciated.

Following is snippets of the code.

In the parent class:

- (void)loadView {

// Some code...
    CGRect buttonFrameRect = CGRectMake(0,yOffset+1,screenRect.size.width,KButtonViewHeight);
    myButtonView = [[UIView alloc]initWithFrame:buttonFrameRect];
    myButtonView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:myButtonView];

// some code... 
    CGRect nxtButtonRect = CGRectMake(screenRect.size.width - 110, 5, 100, 40);
    myNxtButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myNxtButton setTitle:@"Submit" forState:UIControlStateNormal];
    myNxtButton.frame = nxtButtonRect;
    myNxtButton.backgroundColor = [UIColor clearColor];

    [myNxtButton addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [myButtonView addSubview:myNxtButton];

    CGRect backButtonRect = CGRectMake(10, 5, 100, 40);
    myBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myBackButton setTitle:@"Back" forState:UIControlStateNormal];

    myBackButton.frame = backButtonRect;
    myBackButton.backgroundColor = [UIColor clearColor];
    [myBackButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [myButtonView addSubview:myBackButton];
// Some code... 
}

In the child class:

- (void)loadView {

    [super loadView];


//Some code ..

    CGRect buttonViewRect = myButtonView.frame;
    buttonViewRect.origin.y = yOffset; // This is basically original yOffset + 50
    [myButtonView setFrame:buttonViewRect];

    yOffset += KButtonViewHeight;

// Add some other view below myButtonView ..    
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch