简体   繁体   English

如何以及在何处检测到自定义UIView大小动态更改并相应地调整滚动视图?

[英]How and where to detect that a custom UIView size dynamically changed and adjust a scrollview accordingly?

iOS 9+, Swift 3.0 iOS 9 +,Swift 3.0

There are three classes: 有三类:

  1. CustomTextField . CustomTextField It can have two states: error and normal . 它可以有两种状态: errornormal This class inherits from UIView. 此类从UIView继承。 It has a stackView . 它具有一个stackView That stackView has a UITextField and a UILabel that displays an error if needed. stackView有一个UITextField和一个UILabel ,如果需要,该stackView会显示错误。 The validation and behaviour in case of error is hidden in this class. 发生错误时的验证和行为将隐藏在此类中。 When there is an error, the stackView get's the UILabel view added and it grows in height. 出现错误时,将添加stackView的UILabel视图,并且其高度会增加。
  2. BaseViewController , it has a scrollView. BaseViewController ,它具有一个scrollView。 It implements the behaviour of scrolling up a view if a keyboard appears. 如果出现键盘,它实现了向上滚动视图的行为。
  3. ImplementationViewController . ImplementationViewController It inherits from BaseViewController and has a property of type CustomTextField . 它继承自BaseViewController并具有CustomTextField类型的属性。

When in the ImplementationViewController a user taps on the CustomTextField , a keyboard comes up and the CustomTextField is scrolled up right above the keyboard. 当用户在ImplementationViewController点击CustomTextField ,会出现一个键盘,并且CustomTextField会在键盘上方向上滚动。

When the keyboard is visible, if user types an erroneous text, then an error slides down from the bottom of the CustomTextField . 当键盘可见时,如果用户键入错误的文本,则错误将从CustomTextField的底部向下滑动。

How and where to implement the behaviour to move the scroll view up when the CustomTextField shows an error; CustomTextField显示错误时,如何以及在何处实现向上滚动视图的行为; and move the scroll view down when the error is being hidden? 并在隐藏错误时向下移动滚动视图?

Create a protocol CustomTextFieldDelegate and inside it add a method call it InformImplemntation and declare delegate variable in CustomTextField class 创建一个协议CustomTextFieldDelegate并在其中添加一个名为InformImplemntation的方法,并在CustomTextField类中声明委托变量

CustomTextField().delegate = self [ImplementationViewController] CustomTextField()。delegate = self [ImplementationViewController]

and when there is an error or not call delegate method and from ImplementationViewController as it inherits from BaseViewController manage whether to hide/show keyboard 当有错误或没有调用委托方法时,从BaseViewController继承的ImplementationViewController中管理是否隐藏/显示键盘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM