简体   繁体   English

如何获取泛型项目 swift 的高度

[英]How to get the height of generic type item swift

I have a function (in an UIViewController extension) which needs to take a generic type (because I could be passing in a button, a UITextField, etc).我有一个 function (在 UIViewController 扩展中)需要采用通用类型(因为我可以传入一个按钮、一个 UITextField 等)。 In that function I need to find the heigh and width of the generic type (button, UITextField, etc).在那个 function 中,我需要找到通用类型(按钮、UITextField 等)的高度和宽度。 I am using this info to be able to scroll up the page based on the keyboard height to make the button or whatever it is visible.我使用此信息能够根据键盘高度向上滚动页面以使按钮或任何可见的按钮。 The code for it is down below.它的代码在下面。

extension UIViewController {
       func scrollUpKeyboard<Object>(object: Object, keyboardHeight: CGFloat, realOrigin: 
           CGPoint) {
       var buttonHeight = self.view.frame.size.height - (object.frame.size.height + 
           realOrigin.y)
        
        if keyboardHeight > buttonHeight {
            self.ScrollUpFromKeyboard(amount: keyboardHeight - buttonHeight)
        } else {
             self.ScrollUpFromKeyboard(amount: -10)
             
        }
   }
// More code
}

The error I'm getting is "Value of type 'Object' has no member 'frame'."我得到的错误是“'对象'类型的值没有成员'框架'。” How do I get the height and width of the generic object.如何获得通用 object 的高度和宽度。

These aren't generic objects.这些不是通用对象。 They're all UIViews, which is a superclass.它们都是 UIView,它是一个超类。 You can just pass that:你可以通过它:

func scrollUpKeyboard(object: UIView, ...

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

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