ScrollView中最后一个元素的坐标 [英] Coordinates of last element in ScrollView

查看:174
本文介绍了ScrollView中最后一个元素的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动视图,其中有许多元素的高度是可变的。



我想知道是否有一种方法可以知道最后一个的maxY滚动视图中的元素,而没有累加滚动视图中所有元素的高度。



我尝试了lastViewInScrollView.frame.maxY,这是不对的。

解决方案

这完全是数学,就像您说的那样,您不想在最后一个元素之前添加所有元素,那就可以了,现在可以结束滚动视图。
根据Apple文档(



应对UIScrollView的约束:


  1. scollview约束:



    a)导致超级视图:0 // superview(VC mainView)



    b)尾随即可观看w:0



    c)从顶部到视图:0



    d)从底部到视图:0


  2. 在滚动视图中保留一个uiview(说myView)并给出约束:



    a)导致scollView: 0



    b)拖到scollView:0



    c)顶部到scollView:0



    d)底部到scollView:0



    e)与mainview的宽度相等//假设您不希望水平滚动,否则,不要给


  3. 现在在myView中添加view1,添加约束
    a)导致myView:0



    b)尾随myView:0



    c)顶至myView:0



    d)常量height = height1


现在,view1.frame =(0,0,scW,height1)// scW是scrollViewWidth ,而您无需设置此框架。我只是在这里向您描述view1的框架。


  1. 现在在myView中添加view2,添加约束



    a)导致myView:0



    b)尾随myView:0



    c)顶部到视图1:0



    d)恒定高度=高度2












重复相同的操作,直到view9,


  1. 现在在myView中添加view10,添加约束



    a)导致myView:0



    b)跟踪myView:0



    c)返回顶部to view9:0



    d)恒定高度= height2



    e)底部到myView:0


如果您遇到如何设置框架的问题,特别是当它们不在控制器视图中时,请选择VC转到其属性并设置大小自由度并设置VC的任何高度,以便您可以设置l框架。



注意:永远不要忘记给滚动视图设置静态高度,滚动视图本身会计算其高度通过计算其组件的高度来确定高度,但是请记住,您必须给其最上方的组件顶部常量赋予其超级视图,并降低最下面的组件底部组件和所有组件的高度(此处为UIView)。



尝试执行上述步骤,对于新的i滚动视图是个好习惯。



如何设置COLLIE IE的动态高度



在上面的同一示例中,存在三个视图(aView,bView,cView),并且高度是可变的(因为我们在设计时不知道它们的高度,但我们知道最小高度,请指定Ha,Hb,Hc),它们在view2中。



view2的contriants已由我们提供,现在将其高度关系从 equalto(=)更改为 greaterthanequalto(> =)


  1. aView的约束:



    a)顶部到视图2:0



    b)导致视图2:0



    c)尾随view2:0



    d)恒定高度:Ha,与关系 greaterthanequalto(> =)


  2. bView的约束:



    a)顶部到aView:0



    b)导致view2:0



    c)尾随view2:0



    d)恒定高度:Hb,与 greaterthanequalto(> =)

  3. 相关:
  4. cView的约束:



    a)顶部到bView:0



    b)导致view2:0



    c)尾随view2:0



    d)恒定高度:Hc,与关系 greaterthanequalto(> =)



    e)底部到view2:0


注意每当处理滚动视图时,都必须执行相同操作,如果您的组件是动态的,则给定高度(保持最小高度)关系 greaterthanequalto(> =),如果您的组件具有静态的高度,则给定关系 equalto( =)


I have this scroll view with many elements inside for which the height is variable.

I want to know if there is a way to know the maxY of the last element in the scrollview, without adding up the height of all the elements in the scrollview.

I tried lastViewInScrollView.frame.maxY and it was not right.

解决方案

It is totally mathematics, as you said you dont want to add all the element before the last element, thats OK, now work with the end of the scrollview. As per the apple documentation (https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html), to properly deal with the scrollView you have to add a UIView (let says myView)and then your components.

Assuming you have scrollview which contains 10 UIView (view1, view2, ... view10 ) of different height and your viewController is like below:

Dealing with Constraints of UIScrollView:

  1. scollview constraints :

    a) leading to superview : 0 //superview(VC mainView)

    b) trailing to superview : 0

    c) top to superview : 0

    d) bottom to superview : 0

  2. keep a uiview(says myView) inside the scrollview and give consraints:

    a) leading to scollView : 0

    b) trailing to scollView : 0

    c) top to scollView : 0

    d) bottom to scollView : 0

    e) equal width to the mainview // assuming as you dont wanted horizontal scrolling otherwise, dont give

  3. Now add view1 in myView, add contraints a) leading to myView : 0

    b) trailing to myView : 0

    c) top to myView : 0

    d) constant height = height1

now, view1.frame = (0,0,scW,height1) // scW is scrollViewWidth, and you dont need to set this frame. I just write here to describe you the frame of view1.

  1. Now add view2 in myView, add contraints

    a) leading to myView : 0

    b) trailing to myView : 0

    c) top to view1 : 0

    d) constant height = height2

    :

    :

    :

    :

    :

Repeat same to upto view9,

  1. Now add view10 in myView, add contraints

    a) leading to myView : 0

    b) trailing to myView : 0

    c) top to view9 : 0

    d) constant height = height2

    e) bottom to myView : 0

If you facing problem how to set frames, specially when they are out of controller view, then select you VC goto its property and set size freedom and set any height of VC so that you can set all the frames. DONT'T FORGET TO SET THIS VC SIZE TO INFERED.

NOTE: Never give the static height to the scrollview, scrollview will itself calculate its height by calculating the height of its components, but keep in mind you have to give top most component top constaraint to its superview and lower most component bottom component and height of all the components (here, UIView).

Try this above procedure it is good practice for new i scrollview.

HOW TO SET DYNAMIC HEIGHT OF SCOLLVIEW I.E. SOME VIEWS ARE CHANGING THERE HEIGHTS ACCORDING TO THERE CONTENTS

Let in the same above example, there are three views (aView, bView, cView) and there height is variable (as we dont know their height during designing, but we know minimum height let Ha, Hb, Hc respec.) and they are in view2.

Contriants of view2 is already given by us, now change its height relation from equalto(=) to greaterthanequalto(>=) .

  1. Constraint of aView :

    a) top to view2 : 0

    b) leading to view2 : 0

    c) trailing to view2 : 0

    d) constant height : Ha, with relation greaterthanequalto(>=)

  2. Constraint of bView :

    a) top to aView : 0

    b) leading to view2 : 0

    c) trailing to view2 : 0

    d) constant height : Hb, with relation greaterthanequalto(>=)

  3. Constraint of cView :

    a) top to bView : 0

    b) leading to view2 : 0

    c) trailing to view2 : 0

    d) constant height : Hc, with relation greaterthanequalto(>=)

    e) bottom to view2 : 0

NOTE Whenever you are dealing with scrollview you have to do the same, if your component is dynamic give height(keep minimun height) relation greaterthanequalto(>=) and if your component have static height give relation equalto(=)

这篇关于ScrollView中最后一个元素的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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