[英]Swift- how to position an item when working with a UIScrollView
所以我目前有一个UIScrollView,它允许用户垂直向下滚动页面。 我还有一个UIBarButton,当单击该UIBarButton时,它会在页面的中心创建一个按钮,但是由于UIScrollView,我无法告诉该按钮简单的CGRectMake坐标,因为屏幕的“中心”取决于用户的距离向下滚动。 我没有使用故事板。
非常感谢
如果按钮不需要在滚动视图内部,则可以通过使用按钮的中心将其添加到滚动视图的超级视图中
theButton.frame = CGRect(x:scrollView.center.x ,y: scrollView.center.y ,width:buttonWidht, height: buttonHeight)
scrollViewSuperView.addSubview(theButton)
如果需要在滚动视图中,则需要计算当前的y中心。 您可以通过将scrollView的高度添加到当前scrollOffset并将其除以2来实现此目的
let currYCenter = (scrollView.contentOffset.y + scrollView.frame.size.height) / 2
theButton.frame = CGRect(x:scrollView.center.x ,y: currYCenter ,width:buttonWidht, height: buttonHeight)
scrollView.addSubview(theButton)
如果我对您的问题理解正确,则应使用contentOffset
; UIScrollView
此属性返回用户向下/向右滚动多少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.