ios UIButton在UIScrollView中不工作

hgncfbus  于 2023-06-07  发布在  iOS
关注(0)|答案(9)|浏览(255)

我的观点结构:

UITableView
  UITableViewCell
    UIScrollView
      CustomView
        UIButton

问题是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];

我还添加了这个:

scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;

但我的按钮不管用。我不能取消ContentTouches或为UITableView设置延迟,因为如果我设置它,UITableView停止垂直滚动。
感谢您的任何帮助!

qqrboqgw

qqrboqgw1#

将CustomView中的所有子视图添加到ScrollView。并隐藏CustomView。确保CustomView(scrollView的ContainerView)也应该是scrollView的子视图。

bpzcxfmw

bpzcxfmw2#

按钮在滚动视图中不起作用。我花了很多时间与此斗争,并寻求帮助。最后我找到了这个答案:Add buttons to UIScrollView Swift 5 programmatically,其中UIStackView使事情变得简单。
我对解决方案的修改是这样的:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let subView = UIView()
    view.addSubview(subView)
    subView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        subView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0),
        subView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0),
        subView.widthAnchor.constraint(equalToConstant: 400),
        subView.heightAnchor.constraint(equalToConstant: 250),
    ])
    
    let scrollView = UIScrollView()
    subView.addSubview(scrollView)
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: subView.topAnchor, constant: 0),
        scrollView.bottomAnchor.constraint(equalTo: subView.bottomAnchor, constant: 0),
        scrollView.leadingAnchor.constraint(equalTo: subView.leadingAnchor, constant: 0),
        scrollView.trailingAnchor.constraint(equalTo: subView.trailingAnchor, constant: 0),
    ])
    
    let stackView = UIStackView()
    scrollView.addSubview(stackView)
    stackView.axis = .vertical
    stackView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        stackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 8.0),
        stackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 8.0),
        stackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -8.0),
        stackView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -8.0),
    ])
    
    // Create what ever content you need to be shown in the scroll view.
    // In this example long view with a button somewhere in the middle.
    let content = UIView()
    stackView.addArrangedSubview(content)
    content.backgroundColor = .gray
    content.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        content.widthAnchor.constraint(equalTo: subView.widthAnchor, constant: -10),
        content.heightAnchor.constraint(equalToConstant: 1000.0),
    ])
    
    let button = UIButton(type: .system)
    button.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
    button.backgroundColor = .blue
    button.setTitle("Button", for: .normal)
    button.center = CGPoint(x: 100, y: 400)
    content.addSubview(button)
    button.addTarget(self, action: #selector(click), for: .touchDown)
}

@objc func click(_ sender : UIButton) {
    print("Click")
}
vxf3dgd4

vxf3dgd43#

虎克船长的链接帮了我。我使用UIGestureRecognizer而不是UIButtons。
Allow UIScrollView and its subviews to both respond to a touch使用它,如果你有类似的问题。

pkwftd7m

pkwftd7m4#

我有同样的问题&相同的层次结构的视图,与最新的sdk,只是使用它:
将同一UITableViewCell中的UIButton的delaysContentTouches设置为NO。

self.tableView.delaysContentTouches = NO
vc9ivgsu

vc9ivgsu5#

创建UIScrollview的子类

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
  return NO;
}

如果没有收到该消息,请设置:

scrollView.delaysContentTouches = NO;

问题是由于touchesShouldCancelInContentView默认的行为方式,它只在子视图是UIControl时返回NO,但您的按钮隐藏在CustomView中,而不是。

uxhixvfz

uxhixvfz6#

也遇到了同样的情况。UIScrollView在单独的类中,并通过自定义视图添加按钮。“NSNotificationCenter”帮助我解决了这个问题。样本代码:

-(void)tileTouchUpInside:(id)sender
{    
    NSDictionary *dict=[NSDictionary dictionaryWithObject:@"index" forKey:@"index"];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Action" object:nil userInfo:dict];
}

在包含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"];
    // .......
}

为滚动视图、自定义视图和按钮启用用户交互。

xsuvu9jc

xsuvu9jc7#

我猜你需要设置按钮框架。默认情况下,如果使用btn = [[UIButton alloc] init];方法初始化按钮,它将是UIButtonTypeCustom。还设置了一个背景颜色,以调试的目的,记下按钮实际放置的位置。现在,对于您的情况,您需要为该按钮设置框架,如btn.frame = CGRectMake(0,0,100,100);
对于内部UIScrollView,实现此委托方法。

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
  {
       return ![view isKindOfClass:[UIButton class]];
  }

这可能行得通。

1l5u6lss

1l5u6lss8#

没有一个答案对我有效,因为我的问题是按钮在布局时不可见什么的...
修复:
1)将按钮完全移出scrollView(成为VC主view的子视图)。

2)选择button和你的contentV中你想要你的按钮定位的元素(在我的例子中:在conventV中的最后一个元素之后)

3)添加必要的约束(在我的示例中:底部对齐)

4)为button添加其余的约束。
5)好好享受

uklbhaso

uklbhaso9#

CustomView的框架可能会干扰UIButton。它们是重叠的吗?将您的按钮添加到CustomView中,只是为了通过[CustomView addSubview:UIButton];进行测试(当然使用您的值)。如果它起作用,那么你的问题很可能是重叠。

相关问题