简体   繁体   English

如何在Swift中更改MKMapView的大小?

[英]How to change size of an MKMapView in Swift?

I can't seem to change the size of my MKMapView in Swift. 我似乎无法在Swift中更改MKMapView的大小。 How would one go on about it? 怎么会这样呢?

I've tried two different methods but without any luck: 我尝试了两种不同的方法,但是没有任何运气:

var rect: CGRect = self.view.frame;
rect.origin.y = 0;
self.mapView.frame = rect;

and one where I used constraints and autolayout but it made the app crash. 还有一个我使用约束和自动布局的地方,但是它使应用程序崩溃了。 Any ideas? 有任何想法吗?

EDIT: 编辑:

When I write this code it doesn't crash but writes some warnings in the output: 当我编写此代码时,它不会崩溃,但会在输出中写一些警告:

    let height = UIScreen.mainScreen().bounds.height
    let width = UIScreen.mainScreen().bounds.width

    let widthConstraint = NSLayoutConstraint(item: mapView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: width)
    self.view.addConstraint(widthConstraint)

    let heightConstraint = NSLayoutConstraint(item: mapView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: height)
    self.view.addConstraint(heightConstraint)

The output says: 输出显示:

2016-03-09 18:43:02.697 Map[19782:3177712] Unable to simultaneously satisfy constraints. 2016-03-09 18:43:02.697 Map [19782:3177712]无法同时满足约束。 Probably at least one of the constraints in the following list is one you don't want. 以下列表中至少有一个约束是您不想要的约束。 Try this: (1) look at each constraint and try to figure out which you don't expect; 尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束; (2) find the code that added the unwanted constraint or constraints and fix it. (2)查找添加了一个或多个不必要约束的代码并进行修复。 ( "", "" ) (“”,“”)

Will attempt to recover by breaking constraint 将尝试通过打破约束来恢复

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。 Message from debugger: Terminated due to signal 15 来自调试器的消息:由于信号15而终止

I don't really understand this, any ideas? 我真的不太明白,有什么想法吗?

In this case you should use 在这种情况下,您应该使用

self.mapView.addConstraint(widthConstraint)

and

self.mapView.addConstraint(heightConstraint)

You are trying to add a constraint that belongs to the mapView to its superview, that's not the way it's supposed to be. 您试图将属于mapView的约束添加到其超级视图中,这不是应该的方式。 If you were adding a vertical distance between the mapview and another view you would add it to the superview, but not in this case. 如果要在地图视图和另一个视图之间添加垂直距离,则可以将其添加到超级视图中,但在这种情况下不可以。

For the constraint warning you try to add constraint without disabling the autorisizing mask into constraint translation. 对于约束警告,您尝试添加约束而不禁用自动提升掩码到约束转换中。

Try adding this line: 尝试添加以下行:

mapView.translatesAutoresizingMaskIntoConstraints = false

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM