以编程方式在Swift中添加约束不起作用

时间:2015-12-17 09:59:19

标签: ios swift autolayout uikit

我添加了以下代码,以便以编程方式添加视图:

let horizontalConstraint = NSLayoutConstraint(item: newView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)

它不起作用。视图不居中。它仍然在左边。

编辑:

    override func viewDidLoad() {
    super.viewDidLoad()

    newView = LineChartView(frame: CGRectMake(0, 0, 50, 50))
    newView?.delegate = self
    newView?.drawBordersEnabled = true
    newView?.noDataText = "No Data"
    newView?.noDataTextDescription = "No Data"
    newView?.borderColor = UIColor.blackColor()

    self.view.addSubview(newView!)

1 个答案:

答案 0 :(得分:1)

您需要选择我朋友的一方,如果您使用自动布局,请不要使用private void onFbLogin() { LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_friends")); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { System.out.println("Success"); if (ShareDialog.canShow(ShareLinkContent.class)) { File sdCard = Environment.getExternalStorageDirectory(); File file = new File(sdCard, "final_card.jpg"); ShareLinkContent content = new ShareLinkContent.Builder()// .setContentUrl(Uri.parse("file:///sdcard/sharing_event.jpg")) .setContentDescription("some desc") .setContentTitle("Chennai") .build(); shareDialog.show(content); } } @Override public void onCancel() { showAlert(); } @Override public void onError(FacebookException error) { showAlert(); } private void showAlert() { new AlertDialog.Builder(FinalCardView.this) .setTitle(getString(R.string.cancelled)) .setMessage(getString(R.string.permission_not_granted)) .setPositiveButton(getString(R.string.ok), null) .show(); } }); } 初始化您的对象。尝试这样的事情......

frame

如果您的var newView:LineChartView! override func viewDidLoad() { super.viewDidLoad() newView = LineChartView() newView.translatesAutoresizingMaskIntoConstraints = false newView.delegate = self newView.drawBordersEnabled = true newView.noDataText = "No Data" newView.noDataTextDescription = "No Data" newView.borderColor = UIColor.blackColor() self.view.addSubview(newView) let width = NSLayoutConstraint(item: newView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50) let height = NSLayoutConstraint(item: newView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50) newView.addConstraint(width) newView.addConstraint(height) let x = NSLayoutConstraint(item: newView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0) let y = NSLayoutConstraint(item: newView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0) self.view.addConstraint(x) self.view.addConstraint(y) } 对象是LineChartView的子类,那么这应该可行,并且您的超级视图中间应该有一个50x50的对象。

如果你要在代码中做这样的约束,你应该考虑使用苹果Visual Formatting Language