简体   繁体   English

UIButton添加到子视图时不接收触摸,但是添加到主视图时不接收触摸

[英]UIButton not receiving touches when added to subview, but does when added to main view

Basically I have a main screen that comes up with a background image and three menu choices across the bottom of the screen. 基本上,我有一个主屏幕,上面带有背景图像和屏幕底部的三个菜单选项。 When I add these 4 components to the RootViewController's view, everything works fine., but if I do something like 当我将这4个组件添加到RootViewController的视图中时,一切正常,但是如果我做类似的事情

_mainContainer = [[UIView alloc] init];
self.view = _mainContainer;
_firstScreen = [[UIView alloc] initWithFrame:self.view.frame];

"some code here setting up the subviews"

[buttonLeft addTarget:self action:@selector(startGame) forControlEvents:UIControlEventTouchUpInside];

[_firstScreen addSubview:mainMenuBackground];
[_firstScreen addSubview:buttonLeft];

[_firstScreen addSubview:buttonMid];
[_firstScreen addSubview:buttonRight];
[_mainContainer addSubview:_firstScreen];

Essentially if I add everything directly to _mainContainer, the buttons work, if I add it to _firstScreen, then add _firstScreen they don't work. 本质上,如果我将所有内容直接添加到_mainContainer,则按钮会起作用,如果我将其添加到_firstScreen,然后添加_firstScreen则它们将不起作用。

User interaction is enabled on _firstScreen. 在_firstScreen上启用了用户交互。 I do something similar later in my app and it works fine. 稍后我会在我的应用中执行类似的操作,并且效果很好。 Also, not sure if this has anything to do with it, but despite my screen being displayed in landscape, and all options in the plist being set to landscape only, when I run a transition, like flipFromBottom (or whatever its called), my app thinks the "bottom" is with the screen being in portrait until I move to another group of subviews in my app. 另外,不确定是否与它有任何关系,但是尽管我的屏幕显示为横向,并且plist中的所有选项都设置为仅横向,但是当我运行过渡时,例如flipFromBottom(或它的任何名称),我的应用程序认为屏幕底部是“底部”,直到我移到应用程序中的另一组子视图为止。 (So basically the title screen is acting screwy). (因此,标题屏幕基本上是固定的)。

I looked everywhere for a solution, hopefully I am not missing something basic. 我到处都在寻找解决方案,希望我不会缺少一些基本知识。

Hey what is mainMenuBackground?, if its a image , their is no need to make it as a subView, instead, the correct way is to set it as a background image. 嘿,mainMenuBackground是什么?如果它是image,则不需要将其设置为subView,而是将其设置为背景图像是正确的方法。 I hope this will solve your problem 我希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS应用中,即使没有添加主自定义UIView,我仍然可以将其他视图对象添加为子视图吗? - In an iOS app, even when no main custom UIView is added, I can still add other view objects as subview? 添加到inputAccessoryView的UIButton无法检测到触摸 - UIButton added to inputAccessoryView not detecting touches UITextField添加为子视图时不显示 - UITextField not showing when added as subview 当父视图出现时,自定义序列中添加的子视图丢失 - Subview added in custom segue got missing when parent view appears 添加到视图的子视图的UIButton无法响应 - UIButton Added to a View's Subview Won't Respond 当作为子视图添加到UINavigationBar时,UISearchBar不显示“取消”按钮 - UISearchBar does not display Cancel button when added as subview to UINavigationBar 添加到视图,滚动视图和表格单元格时未调用UIbutton方法 - UIbutton method not getting called when added to view, scrollview & tablecell 如果将UIButton作为子视图添加到UIImageView,则单击UIButton不起作用 - Click on UIButton Does Not Work if it's Added as Subview to UIImageView 禁用从子视图上的父视图接收触摸 - Disable receiving touches from parent view on subview UIDatePickerView文本添加为​​子视图时会中断 - UIDatePickerView Text Cutting off when added as Subview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM