ios - (Rubymotion) 如何在 Formotion 中执行此自定义 View ?

标签 ios ruby rubymotion formotion

我目前正在努力使用 Formotion 制作自定义表单(而且我在 Rubymotion 方面还是个新手)。也许我必须尝试另一个框架或者根本不做任何框架....

我试着做这个:

此时我在Formotion中创建了这个:

问题是密码和电子邮件之间的边界。它必须在文本字段下开始,而不是在图标下。这是一个很小的变化,但我不知道如何用 Formotion 做到这一点。那些事情容易吗?提前致谢!

这是我的代码:

class LoginScreen < PM::FormotionScreen

  title ""

  def table_data
    {
      sections: [
        {
          title: "Login",
          rows: [
            {
              title:  "Email",
              key:    :email,
              type:   :email,
              placeholder: "me@mail.com",
              auto_correction: :no,
              auto_capitalization: :none,
              text_alignment: UITextAlignmentLeft
            }, {
              title: "Password",
              key: :password,
              type: :password,
              placeholder: "required",
              secure: true,
              text_alignment: UITextAlignmentLeft
            }
          ]
        }
      ]
    }
  end
end

我覆盖了 EmailRow 以插入图标

motion_require 'string_row'

module Formotion
  module RowType
    class EmailRow < StringRow

      def build_cell(cell)
        cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue
        field = UITextField.alloc.initWithFrame(CGRectZero)
        field.tag = TEXT_FIELD_TAG

        observe(self.row, "value") do |old_value, new_value|
          break_with_semaphore do
            update_text_field(new_value)
          end
        end

        field.clearButtonMode = UITextFieldViewModeWhileEditing
        field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter
        field.textAlignment = row.text_alignment || UITextAlignmentRight

        field.keyboardType = keyboardType

        field.secureTextEntry = true if row.secure?
        field.returnKeyType = row.return_key || UIReturnKeyNext
        field.autocapitalizationType = row.auto_capitalization if row.auto_capitalization
        field.autocorrectionType = row.auto_correction if row.auto_correction
        field.clearButtonMode = row.clear_button || UITextFieldViewModeWhileEditing
        field.enabled = row.editable?
        field.inputAccessoryView = input_accessory_view(row.input_accessory) if row.input_accessory

        add_callbacks(field)

        cell.swizzle(:layoutSubviews) do
          def layoutSubviews
            old_layoutSubviews

            # viewWithTag is terrible, but I think it's ok to use here...
            formotion_field = self.viewWithTag(TEXT_FIELD_TAG)
            formotion_field.sizeToFit

            field_frame = formotion_field.frame
            field_frame.origin.x = self.textLabel.frame.origin.x + Formotion::RowType::Base.field_buffer * 2
            field_frame.origin.y = ((self.frame.size.height - field_frame.size.height) / 2.0).round
            field_frame.size.width = self.frame.size.width - field_frame.origin.x - Formotion::RowType::Base.field_buffer
            formotion_field.frame = field_frame

          end
        end

        if UIDevice.currentDevice.systemVersion >= "6.0"
          field.swizzle(:setText) do
            def setText(text)
              r = old_setText(text)
              self.sendActionsForControlEvents(UIControlEventEditingChanged)
              r
            end
          end
        end

        field.font = BW::Font.new(row.font) if row.font
        field.placeholder = row.placeholder
        field.text = row_value

        icon = UIImage.imageNamed("icons/mail.png")
        icon_view = UIImageView.alloc.initWithImage(icon)
        icon_view.setFrame(CGRectMake(20, 18, icon_view.frame.size.width, icon_view.frame.size.height))

        cell.addSubview(icon_view)
        cell.addSubview(field)
        cell.textLabel.hidden = true
        field

      end

    end
  end
end

最佳答案

这最终非常容易。只需在 EmailRow 的 cell.imageView 中添加一个图像。

添加了这一行:

cell.imageView.image = UIImage.imageNamed("icons/mail.png")
cell.imageView.setFrame(CGRectMake(20, 18, cell.imageView.frame.size.width,       cell.imageView.frame.size.height))

然后您使用以下代码访问单元格,例如:

cell = @form.table.visibleCell.first

关于ios - (Rubymotion) 如何在 Formotion 中执行此自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20327328/

相关文章:

ios - 使用 NSValue 包装一个 C 指针

ios - 在 ViewController 类上设置值

ruby - 使用 Module#module_function 被认为是不好的吗?

iphone - 如何使用 RubyMotion 检测 UISlider 是否被点击?

objective-c - 未找到体系结构 i386 的符号

ruby-on-rails - 我如何为我的应用程序使用多个 Gemfiles?

ruby - 学习Ruby线程-线程完成时触发事件

ios - 将列添加到 Rubymotion Tableview

适用于 iOS 的 Python(如 RubyMotion)

ios - 如何在触发单元格的 IBAction 时更新 UITableViewController 的信息?