简体   繁体   中英

UIButton click not working if subview of UIWIndow

I am creating an iOS library. My library when initiated from AppDelegate will create a button on top of the screen.

I have created the button and it is showing up. But when I click, the target method is not evoked.

This is how I have created the button and assigned its action in the library

- (void) addLoginButton {
UIWindow *appWindow = [[[UIApplication sharedApplication] delegate] window];
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
[loginButton setFrame:CGRectMake((appWindow.frame.size.width/2)-35, 20, 70, 20)];
[loginButton setBackgroundColor:[UIColor blueColor]];
[loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(auLoginClicked) forControlEvents:UIControlEventTouchUpInside];
[appWindow addSubview:loginButton];
if([appWindow.rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController *navCon = (UINavigationController *)(appWindow.rootViewController);
    if (!navCon.navigationBar.isHidden) {
        navCon.navigationBar.layer.zPosition = 0;
    }
}
loginButton.layer.zPosition = 5;
}

- (void) auLoginClicked {
NSLog(@"Hello");
}

I copied your code and have the same result.It's really strange and i am still working on it.I confirm it is the storyboard that caused the result,because i use your code in my project which does not use the storyboard,it works properly.

But please do not use loginButton.layer.zPosition = 5; It is not a proper way .

Here are some advices:

But If are you using UINavigationController Use:

[self.navigationController.view.window addSubview:aView];

If are you using UITabBarController Use:

[self.tabBarController.view.window addSubview:aView];

In AppDelegate you can directly assign a view to window. In appDelegate didFinishLaunchingWithOptions method Use:

[self.window addSubview:aView];

Hope it helps...

Instead of adding your button as a subview of window add this then your button will get action. [appWindow.rootViewController.view addSubview:loginButton];

If you add your view(Button) as a subview of window, then it won't be able to get events from user.That is the reason why your button not responding to event. Debug it you will come to understand:)

If you want that to be on the top of every viewController, then check if UINavigationController is not the initialViewController then create a your own NavigationController and make it as initialContoller. It will make your button will be visible throughout the app(until the user by himself present any other VC)

UIWindow *appWindow = [UIApplication sharedApplication].keyWindow;
[appWindow addSubview:view];

You just need to fetch the keyWindow of the application and add the button as a subview. You don't have to set the zPosition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM