【问题标题】:how to to set button horizontally in a center如何在中心水平设置按钮
【发布时间】:2016-08-12 16:48:47
【问题描述】:

我正在尝试以编程方式创建一个按钮,并希望该按钮从底部略高于该按钮,并从左右两侧留出空间。目前按钮显示是这样的

这里是代码

func showbutton(){
        let button = UIButton(type: .System) // let preferred over var here
        button.frame = CGRectMake(8, 550, 415, 50)

        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Add new request", forState: UIControlState.Normal)
        //button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)
        // Set background color to see if label is centered

        view.addSubview(button)

    }

【问题讨论】:

  • 使用自动布局约束。我使用SnapKit 使基于代码的约束不那么糟糕。
  • 好吧,我需要根据某些条件显示和隐藏按钮,所以我需要以编程方式进行
  • 是的,这就是我提到 SnapKit 的原因——编程约束,但不必编写令人讨厌、复杂的约束语法,您可以使用它们的逻辑、基于块的语法来完成。

标签: ios swift button autolayout


【解决方案1】:

正如我在评论中提到的,我以编程方式使用 AutoLayout(您应该使用 AutoLayout)的首选是 SnapKit

func showbutton(){
    let button = UIButton(type: .System) // let preferred over var here
    button.frame = CGRectMake(8, 550, 415, 50)

    button.backgroundColor = UIColor.greenColor()
    button.setTitle("Add new request", forState: UIControlState.Normal)
    //button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
    // Set background color to see if label is centered

    view.addSubview(button)
    // create the constraints
    button.snp_makeConstraints { (make) -> Void in
        make.centerY.equalTo(view)
    }
}

【讨论】:

    【解决方案2】:

    这将在左侧和右侧放置 8 个点的空间,同时保持所有其他尺寸相同

    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width
    
    button.frame = CGRectMake(8, 550, screenSize.width-16, 50)
    

    【讨论】:

    • 这确实有效,但 iOS 开发的未来肯定在于 AutoLayout。我强烈建议不要在 2016 年调整框架。
    • @brandonscript 是的,显然,尽可能使用 AutoLayout,我只是以最简单的方式回答问题,一切都很好
    【解决方案3】:

    您可以通过调整数字来调整按钮的框架,但您会失去自动布局功能,并且当屏幕旋转时您的按钮看起来不正确。

    改为添加约束,就像在情节提要中使用 NSLayoutContraint 类一样。

    【讨论】:

      【解决方案4】:

      解决方案一:

      通过使用 UIView 框架作为参考

          import UIKit
          class ViewController: UIViewController {
              override func viewDidLoad() {
                  super.viewDidLoad()
                  // Do any additional setup after loading the view, typically from a nib.
      
                  let rect : CGRect = self.view.frame
                  let width  = rect.width
                  let height = rect.height
      
                  let buttonHeight : CGFloat = 50.0
                  let leftMargin : CGFloat = 10.0
                  let bottomMargin : CGFloat = 10.0
      
                  let button  : UIButton = UIButton()
                  button.frame = CGRectMake(leftMargin, height-buttonHeight-bottomMargin, width - 2*leftMargin , buttonHeight)
                  button.backgroundColor = UIColor.greenColor()
                  button.setTitle("Add new request", forState: UIControlState.Normal)
      
                  self.view.addSubview(button)
              }
          }
      

      解决方案 2:

      通过使用 AutoLayout 约束:

          import UIKit
          class ViewController: UIViewController {
              override func viewDidLoad() {
                  super.viewDidLoad()
                  // Do any additional setup after loading the view, typically from a nib.
                  let requiredButton : UIButton = {
                      let button = UIButton()
                      button.backgroundColor = UIColor.greenColor()
                      button.setTitle("Add new request", forState: UIControlState.Normal)
                      return button
                  }()
      
                  // dont worry about frame right now
      
                  self.view.addSubview(requiredButton)
      
                  // set frame using autolayout
      
                  self.view.addContraintsWithFormat("H:|-10-[v0]-10-|", views: requiredButton)
                  self.view.addContraintsWithFormat("V:[v0(50)]-10-|", views: requiredButton)
            [enter image description here][1]
              }
      
              override func didReceiveMemoryWarning() {
                  super.didReceiveMemoryWarning()
                  // Dispose of any resources that can be recreated.
              }
      
      
          }
      
      
          extension UIView{
              func addContraintsWithFormat(format: String, views: UIView...){
                  var viewDictionary = [String: UIView]()
                  for (index,view) in views.enumerate(){
                      let key = "v\(index)"
                      viewDictionary[key] = view
                      view.translatesAutoresizingMaskIntoConstraints = false
                  }
      
                  addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions(), metrics: nil, views: viewDictionary))
              }
          }
      

      两种情况下的输出相同

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-07
        相关资源
        最近更新 更多