【问题标题】:How can I programmatically center UITextview across all devices?如何以编程方式在所有设备上居中 UITextview?
【发布时间】:2017-12-08 03:45:31
【问题描述】:

我当前的代码如下

    class SetupScene: SKScene{
        let myTextField: UITextField = UITextField(frame: CGRect(x: 208, y: 175, width: 230.00, height: 33.00));

    override func didMove(to view: SKView) {
        let background = SKSpriteNode(imageNamed: "pixelBackground")
        background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        background.size = self.size
        background.zPosition = -10
        self.addChild(background)

        self.view?.addSubview(myTextField)

        myTextField.backgroundColor = UIColor.white
        myTextField.text = "Enter your name:"
        myTextField.borderStyle = UITextBorderStyle.line
        myTextField.textAlignment = .center
        myTextField.font = UIFont(name: "04b_25", size: 25)

    }
}

上面的代码 myTextField 位于我屏幕的左上角(我的屏幕是横向的)。我可以通过在 x 和 y 值中输入数字来手动将其移动到我想要的位置,但随后它会根据设备移动到不同的位置。当我尝试这样做时:

    myTextField = UITextField(frame: CGRect(x: self.size.width/2, y: self.size.height/2, width: 230.00, height: 33.00))

屏幕上缺少 myTextField。如何像在 SpriteKit 中一样在所有设备上居中:(x: self.size.width/2, y: self.size.height/2)

*请注意,我想以编程方式执行此操作,而不是使用 Storyboards

【问题讨论】:

    标签: ios swift uitextfield uikit


    【解决方案1】:

    不要使用框架。使用自动布局。

    将视图(文本字段)上的translatesAutoresizingMaskIntoConstraints 设置为false,然后使用X 轴NSLayoutAnchor 和Y 轴NSLayoutAnchor 使视图的中心与其父视图的中心相等。

    【讨论】:

    • 经过几次编辑后,我不小心上传了无法显示的代码。现在我编辑了代码以显示它在左上角时的样子。更新后的代码我还应该使用 NSLayout Anchor 吗?
    • @ItsSgtMarv 如果不想使用NSLayoutAnchor,可以使用myTextField.center = self.center..
    • @Daniel,该解决方案很脆弱。如果您操纵视图的框架或中心,AutoLayout 系统可能会根据它对视图应该定位的位置的猜测来覆盖您的视图定位。
    • @ItsSgtMarv,是的,你仍然应该使用 NSLayoutAnchor
    【解决方案2】:

    创建一个自定义类并在需要的地方全局使用它。

    class CustomTextField: UITextField {
        override init (frame : CGRect) {
            super.init(frame : frame)
        }
         func setUp() {
        //Setting constraints of CustomTextField with centerX, centerY
        self.translatesAutoresizingMaskIntoConstraints = false
        self.centerYAnchor.constraint(equalTo: (self.superview?.centerYAnchor)!).isActive = true
        self.centerXAnchor.constraint(equalTo: (self.superview?.centerXAnchor)!).isActive = true
        //use if you want fixed width
        self.widthAnchor.constraint(equalToConstant: 250).isActive = true
        //self.heightAnchor.constraint(equalToConstant: 250).isActive = true
        self.backgroundColor = .red
        self.textAlignment = .center
    }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    class ViewController: UIViewController{
        var demoText = CustomTextField()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.addSubview(demoText)
            demoText.setUp()
            demoText.text = "hello"
        }
    }
    

    输出

    【讨论】:

    • 自定义分类只会增加麻烦,我建议您为设置功能创建一个扩展以使其保持简单
    猜你喜欢
    • 2015-03-10
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2011-12-19
    • 2019-03-05
    • 2013-04-19
    • 2013-07-08
    相关资源
    最近更新 更多