【问题标题】:How to make the button centre aligned in all ios devices programmatically in swift 3?如何在swift 3中以编程方式使所有ios设备中的按钮中心对齐?
【发布时间】:2018-12-15 13:08:08
【问题描述】:

我正在创建一个应用程序,我在其中以编程方式创建了一个按钮,但是当在其他设备(如 iphone8、7 等)中查看时,我遇到的问题是该按钮无法完美显示在中心...它显示在 iphone X 上完美显示。如何在所有设备上完美显示我的按钮?

这是我的代码:

let button = RideRequestButton()

let dropoffLocation = CLLocation(latitude: 37.6213129, longitude: -122.3789554)
let builder = RideParametersBuilder()
builder.dropoffLocation = dropoffLocation
builder.dropoffNickname = "Your Dropoff Location"
button.rideParameters = builder.build()

button.center = view.center
view.addSubview(button) 

【问题讨论】:

    标签: ios swift uibutton autolayout


    【解决方案1】:

    在这一行之前

    button.frame = CGRect(x:0,y:0,width:view.frame.width,height:50)
    button.center = view.center
    

    你必须给按钮一个框架

    【讨论】:

    • 干得好!我明白了。它只需要一个框架。谢谢@Sh_Khan
    【解决方案2】:

    您可以使用自动布局约束使按钮居中。

        button.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(button)
        let centerXConstraint = NSLayoutConstraint(item: button, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
        let centerYConstraint = NSLayoutConstraint(item: button, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
        NSLayoutConstraint.activate([centerXConstraint, centerYConstraint])
    

    或者你可以设置一个自动调整大小的掩码

        button.center = view.center
        button.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin, .flexibleRightMargin, .flexibleBottomMargin]'
    

    或者,如果您的目标是 iOS 9 及更高版本,您可以使用 NSLayoutAnchor

        button.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(button)
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    

    【讨论】:

    • 它在我的button.center = view.center 下方,对吧?
    • 对于自动布局示例,它将代替您的 button.center 代码。 addConstraint 调用添加了 NSLayoutConstraints,这将使按钮保持居中
    • 有趣的方式...谢谢你的帮助。
    • Apple 不鼓励使用 addConstraint 方法。来自 Apple 的文档:When developing for iOS 8.0 or later, set the constraint’s isActive property to true instead of calling the addConstraint(_:) method directly. The isActive property automatically adds and removes the constraint from the correct view. 您也可以使用NSLayoutConstraint.activate([centerXConstraint, centerYConstraint]) 同时激活它们。 developer.apple.com/documentation/uikit/uiview/…
    • @TylerTheCompiler 感谢您的提醒,我不知道这是不鼓励的。我多年来一直在使用 addConstraint 并没有产生任何不良影响,不知道为什么 Apple 会在不弃用 addConstraint 方法的情况下推荐切换,但我会遵循建议。更新了我的帖子以反映文档。
    【解决方案3】:

    如果您想以编程方式在视图中心设置按钮,则可以使用 center 属性或通过设置框架:

    使用中心属性

        let button = RideRequestButton()
        button.backgroundColor = UIColor.gray
        button.frame.size.width = 200
        button.frame.size.height = 50
        button.center = self.view.center
        view.addSubview(button)
    

    通过 CGRect 使用设置框架

        let button = RideRequestButton()
        button.backgroundColor = UIColor.gray
        button.frame.size.width = 200
        button.frame.size.height = 50
        button.frame = CGRect(x: self.view.frame.size.width/2 - button.frame.size.width/2, y: self.view.frame.size.height/2 - button.frame.size.height/2, width: button.frame.width, height: button.frame.height)
        view.addSubview(button)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2011-12-19
      相关资源
      最近更新 更多