• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用自动布局管理 UITextField 的动态位置

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:15:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我的客户要求如下:

我将创建包含大约 15 个文本字段的注册表单(在服务器上预定义)。注册字段中的字段信息来自服务器。在某些情况下,某些字段可能不需要。因此,我将获得该特定案例所需的字段。

例如假设服务器上的预定义字段数为 15。即名字、姓氏、电话号码、出生日期、图片、电子邮件、地址、邮政编码等。

但在某些情况下,电子邮件 ID 字段可能不是必需的。所以我不会从服务器获取该字段。因此,应用程序端我必须从预定义的字段列表中隐藏电子邮件文本字段。

也有可能不需要多个(任何字段)字段。所以我也必须隐藏它们。我试图通过图像表示来解释情况如下。

案例 1:

enter image description here

案例 2:

enter image description here

在这种情况下,我正在做的是(应用程序端)我为所有预定义字段创建文本字段。然后我隐藏了根据服务器响应不需要的文本字段。即电子邮件文本字段或任何其他文本字段。

现在我的问题是,如果不需要任何字段,那么应该重新定位文本字段,并且我在我的应用程序中使用自动布局。我正在隐藏电子邮件文本字段然后如何设置下一个文本字段的位置,例如联系号码、出生日期等?



Best Answer-推荐答案


是的,这完全可以通过自动布局实现。

这里最重要的是:

您不需要手动重新定位,这项工作将由自动布局完成。

你必须做以下事情:

  1. 使用 UIScrollView 并在其中添加所有 UITextField。以便轻松管理内容大小和更改位置。这里 scrollView 的内容大小也是由 Auto layout 管理的,所以不要手动操作。
  2. 不要为 UITextField 手动分配框架。例如在创建 textField 时使用 CGRect
  3. 使用 VFL(视觉格式化语言)。这是一种功能强大的自动布局语言,可让您使用代码在运行时进行动态更改。
  4. 为所有 UITextFields 添加约束,并将 hidden 属性初始设置为 No/False。通过简单地调用以下方法,从您的服务器响应和查询中使它们可见并更新自动布局约束。

    // Change hidden property based on your response
    // ........
    // ........
    // Below methods will update auto layout
    
    [textField layoutIfNeeded];
    [self.view layoutIfNeeded];
    

    您可以将这些方法放在动画 block 中以获得丰富的 UI 体验。

注意:请记住,VFL 不是一个简单的部分,您在使用它时必须小心。


添加可以帮助您的示例代码(但它是在 Swift 中将其转换为 Objective C),此外,VFL 的参数和值可能会有所不同,因为此代码符合我的要求。看看并根据您的需要进行更改。

使用 VFL 创建 UIScrollView:

// Create scrollview to display content
self.scrollView = UIScrollView()
self.scrollView.delegate = self
self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.scrollView)

// Visual formatting constraints of scrollview horizontal-vertical alignment with respect to view
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["scrollView" : scrollView]))

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["scrollView" : scrollView]))

UIScrollView中添加UITextField:

func createRegistrationControls() {
    var previousTextField : UITextField! = nil

    // Remove all pervious views.
    for view in self.scrollView.subviews {
        view.removeFromSuperview()
    }

    for <YOUR NO OF TEXTFIELDS CONDITION> {
        let textField : UITextField! = UITextField()
        textField.borderStyle = UITextBorderStyle.RoundedRect
        textField.font = UIFont.systemFontOfSize(14)
        textField.clearButtonMode = UITextFieldViewMode.WhileEditing        
        textField.setTranslatesAutoresizingMaskIntoConstraints(false)
        textField.delegate = self
        self.scrollView.addSubview(textField)


        // Left constraint 
        self.scrollView.addConstraint(NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.scrollView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 10))

        // Right constraint 
        self.scrollView..addConstraint(NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.scrollView, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: -10))

        // TOP Horizontal constraint
        let metrices = ["width" : self.view.bounds.width]            
        self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[textField(width)]", options: NSLayoutFormatOptions(0), metrics: metrices, views: ["textField" : textField]))

        if previousTextField == nil {
            // Top vertical constraint
            self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[textField]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["textField" : textField]))
        } else {
            // Top constraint to previous view
            self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[previousTextField]-(10)-[textField]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["textField" : textField, "previousTextField" : previousTextField]))
        }
        previousTextField = textField
    }

    if previousTextField != nil {
        // This below constraints will increase UIScrollView content size
        let constraint1 = NSLayoutConstraint.constraintsWithVisualFormat("H:[previousTextField]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["previousTextField" : previousTextField])
        self.scrollView.addConstraints(constraint1)

        let constraint2 = NSLayoutConstraint.constraintsWithVisualFormat("V:[previousTextField]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["previousTextField" : previousTextField])
        self.scrollView.addConstraints(constraint2)
    }

}

关于ios - 使用自动布局管理 UITextField 的动态位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517069/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap