【问题标题】:Position UIButton in UITextview在 UITextview 中定位 UIButton
【发布时间】:2020-01-08 06:08:36
【问题描述】:

我想放置一个“忘记?”按钮进入我的密码文本字段。如果 Textfield 中没有任何内容,则用户应该能够单击它,并且应该会弹出另一个 ViewController。我唯一能做的就是你可以在下面的图片中看到。我的问题是该按钮不可点击,并且它与占位符文本不在同一级别。关于如何解决这两个问题的任何想法?

    let button = UIButton(type: .custom)
    button.setTitle("vergessen?", for: .normal)
    button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
    button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
    passwordTextField.rightView = button
    passwordTextField.rightViewMode = .unlessEditing

【问题讨论】:

  • 为函数self.refresh添加代码
  • 你可以使用stackview。我会试着举个例子。
  • @chirag90 我已经有了这个: IBAction func refresh(_ sender: Any) { } 但它没有连接到任何东西。我必须在哪里调用 self.refresh 以及如何将 IBAction 连接到 Button?
  • 如何更改 textFont 和 Size?

标签: ios swift uibutton uitextfield


【解决方案1】:

在您从我的answer 子类化的文件中,在该文件中添加另一个函数

// Modify the values as required 
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
    let offset = -20
    let width  = 100
    let height = width
    let x = Int(bounds.width) - width - offset
    let y = offset
    let rightViewBounds = CGRect(x: x, y: y, width: width, height: height)
    return rightViewBounds
}

现在您可以删除跟随行

button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))

输出

关于按钮点击事件。删除您的代码,因为您提到它未连接

IBAction func refresh(_ sender: Any) { }

并在创建按钮的同一文件中添加以下代码。

@objc func refresh() {
    // your vc code here
    print("in refresh")
}

上述代码与您拥有的 addTarget 代码挂钩。

button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)

希望这会有所帮助。

【讨论】:

  • 再一次,完美的答案!谢谢!您能否也告诉我如何更改文本大小和字体?:D
  • 按钮的文本大小和字体?
  • 是的,你可以做 button.titlelabel.font = .systemFont(sizeOf : 12)
  • 对于自定义字体,你可以做 button.titleLabel.font = UIFont(name: "OpenSans", size: 20.0)
【解决方案2】:

我建议创建一个xib 文件及其相关视图,以查看其中包含按钮的文本项。所以你将来可以在其他地方重用它(这个或另一个项目)

通过定义恒定高度 (100),您将在不同的 iOS 设备中体验到丑陋和错位的 UI。 这是你应该做的:

  1. 为您的自定义 UITextView 定义一个 xib 文件
  2. 为其创建约束,使其宽度和高度由其父级定义。同时定义您忘记的UIButton 相对于您的UITextView
  3. 定义其 (xib) 相关的 UIView
  4. 在您的Storyboard 中使用它

【讨论】:

    【解决方案3】:

    您可以使用Storyboard。为密码TextField 和忘记Button 创建一个帮助视图。

    -设置Helper view的宽高与邮箱TextField相同。

    -在帮助视图中添加TextFieldButton,然后您可以决定密码TextField 宽度和忘记Button 宽度。 - 为TextFieldButton 设置约束

    我设置了绿色以了解辅助视图的作用。

    更新

    我刚刚使用了您的代码,它工作正常。再次检查您的文本字段约束。这是我用的。

    override func viewDidLoad() {
            super.viewDidLoad()
    
            let button = UIButton(type: .system)
            button.setTitle("vergessen?", for: .normal)
            button.setTitleColor(#colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1), for: .normal)
            button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
    
            button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
            textfFeld.rightView = button
            textfFeld.rightViewMode = .unlessEditing
        }
    
        @objc func refresh(_ sender: Any) {
            print("Hello")
        }
    

    我唯一改变的是按钮类型从.custom.system

    【讨论】:

    • 对不起,这有点陌生。 Helper View 到底是什么意思?
    • @Chris 只是为了安排文本字段和按钮。首先放置视图使其尺寸与电子邮件文本字段相同,然后在该视图内放置密码文本字段并忘记按钮。
    • 你的意思是stackview吗?还是什么“观点”?:D
    • @Chris 只是一个普通的 UIView。
    • @Chis 我刚刚更新了我的答案。检查您的 TextField 约束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多