2016-08-12 70 views
0

我试图以编程方式创建一个按钮,并且希望从底部和左右空间稍微有点按钮。目前按钮显示的是这样如何在一个中心水平设置按钮

enter image description here

这里是代码

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) 

    } 
+0

使用AutoLayout约束。我使用[SnapKit](https://github.com/SnapKit/SnapKit)来制作基于代码的约束条件并不容易。 – brandonscript

+0

以及我需要显示和隐藏按钮的基础上,所以我需要以编程方式执行 – hellosheikh

+0

是的,这就是为什么我提到了SnapKit - 编程约束,但不必编写令人讨厌,复杂的约束语法,你可以做到这一点使用它们的逻辑,基于块的语法。 – brandonscript

回答

0

这将会把对左和右8个点的空间,同时保持你的所有其它尺寸相同

let screenSize: CGRect = UIScreen.mainScreen().bounds 
let screenWidth = screenSize.width 

button.frame = CGRectMake(8, 550, screenSize.width-16, 50) 
+0

这是行不通的,但iOS开发的未来绝对在于AutoLayout。我强烈建议不要在2016年调整帧数。 – brandonscript

+1

@brandonscript是的,显然,尽可能使用AutoLayout,我只是以最简单的方式回答问题,这一切都很好 – Loxx

+0

...好东西;) – brandonscript

0

您可以通过调整数调整按钮的框架,但你会失去自动版式功能和屏幕旋转时你的按钮看起来不对。

改为添加约束,就像在故事板中使用NSLayoutContraint类一样。

0

正如我在我的评论中提到,我去到了编程方式使用自动版式(和你应该使用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) 
    } 
} 
0

解决方案1:

通过使用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:

通过使用自动版式约束:

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)) 
     } 
    } 

在这两种情况下输出相同

相关问题