Question

I have(/had) a UITextField subclass which has a custom delete button as a rightView property. This is working fine up until I rotate the device and then the button does not react anymore. I changed my approach and 'cheated' a bit. Now I'm adding the UIButton on top of the UITextField in code. Which also works fine up until I rotate the device. I checked with backgroundColors if there are no other elements on top of the button.

I also tried removing the button and adding it again when the device rotates. It's visible but the action does not trigger. If I start my app in Portrait or in Landscape it works up until I rotate the device.

How I add the button:

if (!btn_delete)
{
    btn_delete = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, (tf_search.frame.size.height / 2) - 8, 17, 16)];
    [btn_delete setImage:[UIImage imageNamed:@"icon_cross_textfield"] forState:UIControlStateNormal];
    [btn_delete addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_delete];
    [self.view bringSubviewToFront:btn_delete];
}
else
{
    [btn_delete removeFromSuperview];
    btn_delete = nil;
}

Method -(void)clearTextField:(UIButton*)sender does not get called after rotation.

Any advice is appreciated.

Was it helpful?

Solution

The UIButton was added to a UIViewController's UIView which was part of a UIView of another UIViewController. They all had clearColour as backgroundColor so the UIButton was still visible but not in the bounds of the superView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top