我开始阅读 Aaron Hillegass 的书来学习 iOS 编程,但我发现有些示例不起作用。也许它们不起作用,因为有新版本的 iOS 和 Xcode。你能向我解释一下为什么这段代码不起作用吗?正在创建按钮;但是,它没有收到压力。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10,50,100,10)];
[button setTitle:@"OK" forState:UIControlStateNormal];
self.window.backgroundColor = [UIColor whiteColor];
[self.window addSubview:button];
[self.window makeKeyAndVisible];
return YES;
}
关于此类问题的热门建议是添加
button.userInteractionEnabled = YES;
但这并没有帮助。
抱歉,如果我的问题很简单,但我花了几个小时在这里和 developer.apple.com 上找到答案。
最佳答案
出于某种原因,按钮顶部有一个 View ,它阻止按钮接收操作(按下)。您所要做的就是添加:
[self.window bringSubviewToFront:button];
这行代码会将您的按钮放在 View 堆栈的顶部。干杯
关于ios - UIButton 未激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35104566/