ios UIButton 在 UIScrollView 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16649639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton does not work when it in UIScrollView
提问by George
My structure of views:
我的观点结构:
UITableView
UITableViewCell
UIScrollView
CustomView
UIButton
The problem is UIButton doesn't work when I touch it. I create it with code:
问题是当我触摸它时 UIButton 不起作用。我用代码创建它:
btn = [[UIButton alloc] init];
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tileTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
btn.layer.zPosition = 1;
[self addSubview:btn];
I've also add this:
我还添加了这个:
scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;
But my button doesn't work anyway. I can't cancelContentTouches or set delay for UITableView because if I set it, UITableView stop scrolling verticaly.
但是我的按钮无论如何都不起作用。我无法取消 ContentTouches 或为 UITableView 设置延迟,因为如果我设置它,UITableView 将停止垂直滚动。
Thanks for any help!
谢谢你的帮助!
采纳答案by Capt. Hook
Create a subclass of UIScrollview with
创建一个 UIScrollview 的子类
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return NO;
}
If you are not getting that message, set:
如果您没有收到该消息,请设置:
scrollView.delaysContentTouches = NO;
The problem is because of the way touchesShouldCancelInContentView
behaves by default, it returns NO only if the subview is an UIControl, but your button is hidden inside the CustomView which isn't.
问题是因为touchesShouldCancelInContentView
默认情况下的行为方式,仅当子视图是 UIControl 时才返回 NO,但您的按钮隐藏在 CustomView 中,而不是。
回答by Siva
Came across the same scenario. UIScrollView in separate class and adding button through a custom view. 'NSNotificationCenter' helped me solving the issue. Sample code:
遇到了同样的场景。UIScrollView 在单独的类中并通过自定义视图添加按钮。“NSNotificationCenter”帮助我解决了这个问题。示例代码:
-(void)tileTouchUpInside:(id)sender
{
NSDictionary *dict=[NSDictionary dictionaryWithObject:@"index" forKey:@"index"];
[[NSNotificationCenter defaultCenter]postNotificationName:@"Action" object:nil userInfo:dict];
}
In the class containing Scroll View:
在包含滚动视图的类中:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doAction:) name:@"Action" object:nil];
}
-(void)doAction:(NSNotification *) notification
{
NSString* value =[notification.userInfo valueForKey:@"index"];
// .......
}
Enable userInteraction for scroll view, custom view and button.
为滚动视图、自定义视图和按钮启用 userInteraction。
回答by IronMan
I guess you need to set the button frame. By default if you use btn = [[UIButton alloc] init];
method to initialize button it will be UIButtonTypeCustom
. Also set a background color to it for debugging purpose to note down where the button is actually placed.
Now for your case you need to set the frame for that button like btn.frame = CGRectMake(0,0,100,100);
我想你需要设置按钮框架。默认情况下,如果您使用btn = [[UIButton alloc] init];
方法来初始化按钮,它将是UIButtonTypeCustom
. 还为其设置背景颜色以用于调试目的,以记下按钮实际放置的位置。现在对于您的情况,您需要为该按钮设置框架,例如btn.frame = CGRectMake(0,0,100,100);
For the inner UIScrollView implement this delegate method.
对于内部 UIScrollView 实现此委托方法。
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return ![view isKindOfClass:[UIButton class]];
}
This may work.
这可能有效。
回答by sangony
Your CustomView's frame could be interfering with your UIButton. Are they overlapping? Add your button to the CustomView just to test by [CustomView addSubview:UIButton];
(using your values of course). If it works, then your issue is most likely an overlap.
您的 CustomView 的框架可能会干扰您的 UIButton。它们重叠吗?将您的按钮添加到 CustomView 只是为了测试[CustomView addSubview:UIButton];
(当然使用您的值)。如果它有效,那么您的问题很可能是重叠的。
回答by Nikola Markovic
None of the answers worked for me, because my problem was that the button was not visible at layout time or something...
没有一个答案对我有用,因为我的问题是按钮在布局时不可见或其他什么......
FIX:
使固定:
1) move the button out of the scrollView
completely (to be subview of VC's main view
).
1)将按钮scrollView
完全移出(成为VC主视图的子视图view
)。
2) select the button
and the element from your contentV
you want your button located (in my case: after the last element in conventV)
2)button
从contentV
你想要你的按钮中选择和 元素(在我的例子中:在 conventV 中的最后一个元素之后)
3) add the necessary constraints (in my case: to be bottom aligned)
3)添加必要的约束(在我的情况下:底部对齐)
4) Add the rest of the constraints for the button
.
4) 添加其余的约束button
。
5) Enjoy.
5)享受。
回答by Jeet
Add all subviews from CustomView to ScrollView. And hide the CustomView. Make sure CustomView(ContainerView of scrollView) should also be a subView of scrollView.
将 CustomView 中的所有子视图添加到 ScrollView。并隐藏自定义视图。确保 CustomView(ContainerView of scrollView) 也应该是 scrollView 的子视图。
回答by George
Link from Capt.Hook helped me. I used UIGestureRecognizer instead of UIButtons.
Capt.Hook 的链接帮助了我。我使用 UIGestureRecognizer 而不是 UIButtons。
Allow UIScrollView and its subviews to both respond to a touchUse it, if you have the similar problem.
允许 UIScrollView 及其子视图都响应触摸使用它,如果您有类似的问题。
回答by Aks
I had same issue & same hierarchy of the views, With latest sdk , just use it :
我有同样的问题和相同的视图层次结构,使用最新的 sdk ,只需使用它:
Setting delaysContentTouches to NO for UIButton in the same UITableViewCell.
在同一个 UITableViewCell 中将 UIButton 的 delaysContentTouches 设置为 NO。
self.tableView.delaysContentTouches = NO