【发布时间】:2017-03-27 08:09:53
【问题描述】:
我正在创建一个 iOS 库。从 AppDelegate 启动时,我的库将在屏幕顶部创建一个按钮。
我已经创建了按钮并且它正在显示。但是当我点击的时候,目标方法并没有被唤起。
这就是我创建按钮并在库中分配其操作的方式
- (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");
}
【问题讨论】:
-
我认为您必须先使用 Uiview,然后在窗口的上层添加。 uiview 应该添加按钮。试试这个
-
不要使用层
loginButton.layer.zPosition = 5; -
使用bringSubViewFront,如果不是。
-
[窗口 setUserInteractionEnabled:YES];我认为这可以工作。并始终添加上层 uibutton
-
你在使用故事板吗?
标签: ios objective-c uibutton