如何在代码中使用自动布局

时间:2018-07-24 06:37:09

标签: ios autolayout

我想使用自动版式制作自定义tableview,因此如何使用自动版式制作。

我认为应该是这样的:-

self.view.addConstraints([
            NSLayoutConstraint(item: square, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
            NSLayoutConstraint(item: square, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64),
            NSLayoutConstraint(item: square, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0),
            NSLayoutConstraint(item: square, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0),
        ])

1 个答案:

答案 0 :(得分:1)

嗨,我在我的一个项目中做了这件事:

这是VC的代码,您还需要为tableview单元创建一个自定义类

Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;


Command command = commands.AddNamedCommand2(_addInInstance, "AddinMultiLineWatch", "AddinMultiLineWatch", "Executes the command for AddinMultiLineWatch", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

if((command != null) && (toolsPopup != null))
{
    command.AddControl(toolsPopup.CommandBar, 1);
}

这是自定义类的代码

import UIKit

class ViewController: UIViewController {

    //MARK:- view life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configureTableView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //Custom method
    func configureTableView(){
        let tblview = UITableView(frame: view.bounds, style: .plain)
        tblview.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
        tblview.delegate = self
        tblview.dataSource = self
        self.view.addSubview(tblview)
        }

    }

extension ViewController: UITableViewDataSource,UITableViewDelegate{

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

         let cell = CustomTableViewCell(style: .default, reuseIdentifier: "CustomTableViewCell")

         if indexPath.row == 0 {
            cell.backgroundColor = .gray
            cell.lblTitle.text = "Title"
            cell.lblDescription.text = "MessageMe"
            return cell
         }
        else if indexPath.row == 1{
            cell.backgroundColor = .green
            cell.lblTitle.text = "TitleTitleTitleTitleTitleTitle"
            cell.lblDescription.text = "MessageMe"
            return cell
         }
         else{
         cell.backgroundColor = .red
         cell.lblTitle.text = "Name"
         cell.lblDescription.text = "MessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessag"
        return cell
        }
    }

}