在Swift中以编程方式向视图添加约束时出错

时间:2016-07-29 13:05:39

标签: ios swift autolayout

我在日志中遇到错误,告诉我在以编程方式向Swift中的视图添加约束时,存在无法同时满足的冲突约束,但UI在运行模拟器时看起来像我想要的那样你可以在截图中看到。左侧按钮的前缘与分段控件的前缘对齐,右侧按钮的后缘与分段控件的后缘对齐。

我认为正是这些限制导致了问题,因为对这些问题进行评论是阻止错误被抛出的原因,但是UI看起来并不像我想要的那样。

有人可以帮我理解我做错了吗?

[HttpPost]
[Authorize]
public JsonResult SetCulture(String cul) {

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cul);
    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cul);

    return Json(true, JsonRequestBehavior.AllowGet);
}
Response.ClearContent();
Response.ContentType = "Application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=report.pdf");
Response.WriteFile(Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), "report.pdf"));
Response.Flush();
Response.End();

}

Screenshot of the layout, even with constraint errors in log

1 个答案:

答案 0 :(得分:3)

您的问题来自于您未能MKMapView frame mapView = MKMapView() 。当你这样创建它时:

0

您将框架设置为0宽度和<NSLayoutConstraint:0x7fd836318800 'UIView-Encapsulated-Layout-Width' H:[MKMapView:0x7fd83351d900(0)]> 高度。然后,自动布局将该帧转换为视图宽度和高度的约束。

列出的一个冲突约束是:

0

[MKMapView:0x7fd83351d900(0)]中的MKMapView表示存在约束,使0的宽度为mapView = MKMapView() ,这当然不是您想要的。

您可以通过在创建地图视图时为其提供正确的框架来解决此问题:

替换:

mapView = MKMapView(frame: UIScreen.mainScreen().bounds)

使用:

UITabBarController

我的答案解决了这个问题,让iOS设置视图,它正确。

上一个答案

我最初无法重现您的问题,但当我将您的viewController放在UIViewController时,我也看到了自动布局错误消息。

为了使其有效,我在 Storyboard 中使用了标准loadView,并将您的viewDidLoad代码移至MKMapView。我将self.view添加为self.view的子视图以及相应的约束,使其与override func viewDidLoad() { super.viewDidLoad() mapView = MKMapView() mapView.delegate = self mapView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(mapView) NSLayoutConstraint.activateConstraints([ mapView.topAnchor.constraintEqualToAnchor(view.topAnchor), mapView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor), mapView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor), mapView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor) ]) //Adding pins to map let firstLocation = CLLocationCoordinate2DMake(5.000000, -5.000000) let secondLocation = CLLocationCoordinate2DMake(-5.000000, 5.000000) coordinates.append(firstLocation) coordinates.append(secondLocation) let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"]) segmentedControl.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5) segmentedControl.selectedSegmentIndex = 0 segmentedControl.translatesAutoresizingMaskIntoConstraints = false segmentedControl.addTarget(self, action: #selector(MapViewController.mapTypeChanged(_:)), forControlEvents: .ValueChanged) view.addSubview(segmentedControl) let margins = view.layoutMarginsGuide let topConstraint = segmentedControl.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8) let leadingConstraint = segmentedControl.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor) let trailingConstraint = segmentedControl.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor) topConstraint.active = true leadingConstraint.active = true trailingConstraint.active = true let userButton = UIButton() view.addSubview(userButton) userButton.translatesAutoresizingMaskIntoConstraints = false userButton.addTarget(self, action: #selector(MapViewController.userButtonSelected(_:)), forControlEvents: UIControlEvents.TouchUpInside) userButton.setTitle("Current Location", forState: .Normal) userButton.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.7) let bTopConstraint = userButton.topAnchor.constraintEqualToAnchor(segmentedControl.bottomAnchor, constant: 8) //Problematic constraint I think let bLConstraint = userButton.leftAnchor.constraintEqualToAnchor(margins.leftAnchor) let bTConstraint = userButton.trailingAnchor.constraintEqualToAnchor(margins.centerXAnchor, constant: -8) bTopConstraint.active = true bLConstraint.active = true bTConstraint.active = true let pinsButton = UIButton() view.addSubview(pinsButton) pinsButton.translatesAutoresizingMaskIntoConstraints = false pinsButton.addTarget(self, action: #selector(MapViewController.pinsButtonSelected(_:)), forControlEvents: UIControlEvents.TouchUpInside) pinsButton.setTitle("Next Pin", forState: .Normal) pinsButton.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.7) let pTopConstraint = pinsButton.topAnchor.constraintEqualToAnchor(segmentedControl.bottomAnchor, constant: 8) //Problematic constraint I think let pLConstraint = pinsButton.leadingAnchor.constraintEqualToAnchor(margins.centerXAnchor, constant: 8) let pTConstraint = pinsButton.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor) pTopConstraint.active = true pLConstraint.active = true pTConstraint.active = true } 的大小相同:

loadView

替代解决方案:

  1. 将您的viewDidLoad代码移至mapView = MKMapView() view = mapView 并删除这些内容:

    view
  2. Storyboard 中,将ViewControllerMKMapView的班级更改为mapView

  3. @IBOutlet设为 @IBOutlet var mapView: MKMapView!

    MKMapView
  4. 连接故事板中的插座。

  5. 设置mapView代理人。 控制 -drag从 Storyboard 中的viewControllermapView.delegate = self 顶部的 ViewController 图标并选择从弹出窗口中委派。您也可以通过调用:

    来挂接委托
    viewDidLoad

    <asp:FormView ID="FormView1" runat="server">