【问题标题】:iOS: Visual format constraints are brokeniOS:视觉格式约束被打破
【发布时间】:2013-07-02 13:51:23
【问题描述】:

我正在尝试在地图上放置一个按钮并将其限制为从左侧开始 10 点,从底部向上 10 点。但是,当我在模拟器中运行它时,它告诉我我的约束被打破了。这是我的代码。我错过了什么?

_map = [[MKMapView alloc] init];
[self setView:_map];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.810166, -86.156708);

_loc = [[TCMLocationPoint alloc] initWithCoordinate:coord title:@"My Location" subtitle:@"My Subtitle"];
[_loc setParent:_map];
[_map addAnnotation:_loc];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 2000, 2000);
[_map setRegion:region animated:NO];

_directionsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_directionsButton addTarget:self action:@selector(getLocation:) forControlEvents:UIControlEventTouchDown];
[_directionsButton setTitle:@"Get Directions" forState:UIControlStateNormal];
[_directionsButton setFrame:CGRectMake(0.0, 0.0, 150.0, 25.0)];

[[self view] addSubview:_directionsButton];

NSDictionary *nameMap = @{@"button" : _directionsButton};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button]-(>=0)-|"
                                                                                     options:0
                                                                                     metrics:nil
                                                                                       views:nameMap];
[[self view] addConstraints:horizontalConstraints];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[button]-10-|"
                                                                                   options:0
                                                                                   metrics:nil
                                                                                     views:nameMap];

[[self view] addConstraints:verticalConstraints];

【问题讨论】:

  • 您是在loadViewviewDidLoad 还是在其他地方这样做?
  • 我正在使用 viewDidLoad
  • 然后,假设您使用的是 NIB 或故事板,您应该将地图添加为 self.view 的子视图,而不是替换 self.view,并为其设置适当的约束.请参阅我修改后的答案。

标签: ios constraints visual-format-language


【解决方案1】:

一些想法:

  1. 我会将translatesAutoresizingMaskIntoConstraints 设置为NO

  2. 您也可以简化您的 VFL,并消除 >=0 的东西。您还可以在 VFL 中设置宽度和高度。

  3. 您也不需要设置frame。让约束设置宽度、高度。

  4. 除非您在 loadView 中执行此操作,否则我建议您将地图添加为子视图,而不是尝试设置 self.view

因此:

_map = [[MKMapView alloc] init];
_map.delegate = self;   // if you've implemented any `MKMapViewDelegate` methods, you should set your delegate
[_map setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_map];
NSDictionary *views = @{@"map" : _map};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[map]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[map]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views]];

// do your stuff where you're adding your annotation here

_directionsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_directionsButton addTarget:self action:@selector(getLocation:) forControlEvents:UIControlEventTouchDown];
[_directionsButton setTitle:@"Get Directions" forState:UIControlStateNormal];
[_directionsButton setTranslatesAutoresizingMaskIntoConstraints:NO];

[[self view] addSubview:_directionsButton];

NSDictionary *nameMap = @{@"button" : _directionsButton};

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[button(150)]"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:horizontalConstraints];

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(25)]-10-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:nameMap];
[[self view] addConstraints:verticalConstraints];

您甚至可能希望将宽度和高度约束的优先级设置为小于 1000,这样如果字体规定按钮应该大一点,您就会让它增长,例如@"H:|-10-[button(150@500)]"@"V:[button(25@500)]-10-|"

【讨论】:

  • 谢谢。我尝试了上述方法,但最终出现了一个例外:'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. MKMapView's implementation of -layoutSubviews needs to call super.'
  • @LoneWolfPR 假设您在viewDidLoad 中执行此操作,不要只将self.view 设置为地图,而是将地图添加为self.view 的子视图,设置适当的约束,这个错误就会消失。
  • 添加地图作为子视图修复了错误。该按钮显示在它应该显示的位置,但现在地图没有显示。编辑-抱歉,刚刚注意到新的约束。现在会添加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
相关资源
最近更新 更多