更改约束布局后不触发UIButton操作 [英] UIButton action is not triggered after constraint layouts changed

查看:107
本文介绍了更改约束布局后不触发UIButton操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在情节提要ViewController上有一个UIButton。当我将数据加载到表单中并且布局发生重大变化时,按钮无法识别触摸动作。



我已经知道,当按钮在滚动视图右侧可见时如果填充了数据,则触摸动作有效。



如果数据太长并且一开始不可见该按钮,则仅当滚动到显示屏时,触摸操作才起作用。



我正在检查按钮上方是否有东西,但是什么也没有。我试图更改按钮的zPosition,但没有解决问题。



可能是什么问题?



<我已经从UIScrollView和UIButton制作了自定义类,以检查touches事件是如何触发的。它表现出相同的行为,这是显而易见的。如果按钮一开始就可见,则将触发UIButton的touchesBegan事件。如果按钮向下移动且一开始不可见,则不会触发,而是调用滚动视图的touchesBegan。



取决于我加载到的数据的大小页面上的按钮有时在开始时就可见,但是表格仍可以滚动一点。在这种情况下,按钮仍然可以正常工作,因此,这种现象似乎并不取决于滚动视图是否在之前滚动,而仅取决于按钮的初始可见性。



是否应该调用任何布局或显示刷新功能以将行为恢复为按钮状态?





该代码部分可确保如果填充的数据需要更大的空间,则为滚动调整contentview的大小。

  func fillFormWithData(){
dispDescription.text = jSonData [0] [ advdescription]
dispLongDescription.text = jSonData [0] [ advlongdesc]
priceandcurrency.text = jSonData [0] [ advprice]! + + jSonData [0] [ advpricecur]!
validatedate.text = jSonData [0] [ advdate]!
contentview.layoutIfNeeded()
让contentRect = CGRect(x:0,y:0,width:scrollview.frame.width,height:uzenetbutton.frame.origin.y + uzenetbutton.frame.height + 50)

contentview.frame.size.height = contentRect.size.height
scrollview.contentSize = contentview.bounds.size
}





好,因此进行了另一次更新。我已将contentview背景色为蓝色,并将scrollview背景色为白色。当我加载数据并调整布局约束的大小时,contentview会按预期调整大小,但是现在,scrollview会移到底部。滚动视图后,它将调整为适合屏幕大小的原始大小。现在,只有当我触摸后面是蓝色的are时,才能识别该按钮。白色的背景无法识别,因此滚动视图似乎正在隐藏该按钮。

解决方案

最后,我有一旦确定滚动视图的contentview将滚动事件的大小调整为原始大小,便找到了另一个解决方案。 (不清楚为什么会这样,但这就是事实。)



所以我不得不在情节提要中的contentview中添加高度限制,并为并在更改内容大小时调整此约束,例如:

  @IBOutlet弱var ContentViewHeight:NSLayoutConstraint! 

func fillFormWithData(){
dispDescription.text = jSonData [0] [ advdescription]
dispLongDescription.text = jSonData [0] [ advlongdesc]
priceandcurrency.text = jSonData [0] [ advprice]! + + jSonData [0] [ advpricecur]!
validatedate.text = jSonData [0] [ advdate]!
contentview.layoutIfNeeded()
让contentRect = CGRect(x:0,y:0,width:scrollview.frame.width,height:uzenetbutton.frame.origin.y + uzenetbutton.frame.height + 50)

contentview.bounds = contentRect
scrollview.contentSize = contentRect.size

-----------这是关键行成功-------
ContentViewHeight.constant = contentRect.size.height
-------------------- --------------------------------------
}

添加后,它可以正常工作。




I have got an UIButton on a storyboard ViewController. When I load data into the form and the layout is significantly changing the button does not recognise the touch action.

I have figured out that when button is visible on the scrollview right after it if filled with data, the touch action works.

If the data too long and the button is not visible at first, just when it is scrolled into the display, the touch action does not work.

I was checking if something is above the button, but nothing. I have tried to change the zPosition of the button, not solved the problem.

What can be the issue?

I have made custom classes from the UIScrollView and the UIButton to check how the touches event triggered. It is showing the same behaviour, which is obvious. If the button is visible right at the beginning, the UIButton's touchesBegan event is triggered. If the button moves down and not visible at the beginning, it is never triggered, but the scrollview's touchesBegan is called instead.

Depending on the size of the data I load into the page sometimes the button is visible at the beginning, but the form can be still scrolled a bit. In this case the button still work, so it seems that this behaviour is not depending on if the scrollview is scrolled before or not, just on the initial visibility of the button.

Is there any layout or display refresh function which should be called to set back the behaviour to the button?

The code portion which ensures that the contentview is resized for the scroll if the filled data requires bigger space.

func fillFormWithData() {
    dispDescription.text = jSonData[0]["advdescription"]
    dispLongDescription.text = jSonData[0]["advlongdesc"]
    priceandcurrency.text = jSonData[0]["advprice"]! + " " + jSonData[0]["advpricecur"]!
    validitydate.text = jSonData[0]["advdate"]!
    contentview.layoutIfNeeded()
    let contentRect = CGRect(x: 0, y: 0, width: scrollview.frame.width, height: uzenetbutton.frame.origin.y+uzenetbutton.frame.height+50)

    contentview.frame.size.height = contentRect.size.height
    scrollview.contentSize = contentview.bounds.size
}

Ok, so another update. I have coloured the contentview background to blue and the scrollview background to white. When I load the data and resize the layout constraints, the contentview is resizing as expected, however now the scrollview is going to the bottom. After I scroll the view it is resizing to the original size which fits the screen. Now the button is only recognised when I touch the are which is blue behind. With the white background it is not recognised anymore, so it seems that the scrollview is hiding the button.

解决方案

Finally, I have found the solution in another chain, once it became clear that the scrollview's contentview is resizing on scroll event to the original size. (Not clear why this is like this, but that is the fact.)

So I had to add a height constraint to the contentview in the storyboard and create an outlet to it and adjust this constraint when the content size is changing, like this:

@IBOutlet weak var ContentViewHeight: NSLayoutConstraint!

func fillFormWithData() {
    dispDescription.text = jSonData[0]["advdescription"]
    dispLongDescription.text = jSonData[0]["advlongdesc"]
    priceandcurrency.text = jSonData[0]["advprice"]! + " " + jSonData[0]["advpricecur"]!
    validitydate.text = jSonData[0]["advdate"]!
    contentview.layoutIfNeeded()
    let contentRect = CGRect(x: 0, y: 0, width: scrollview.frame.width, height: uzenetbutton.frame.origin.y+uzenetbutton.frame.height+50)

    contentview.bounds = contentRect
    scrollview.contentSize = contentRect.size

    ----------- This is the key line to the success ----------
    ContentViewHeight.constant = contentRect.size.height
    ----------------------------------------------------------
}

After this is added, it works perfectly.

这篇关于更改约束布局后不触发UIButton操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆