지도 위에 버튼을 놓고 왼쪽에서 10 포인트, 아래에서 위로 10 포인트를 제한하려고합니다. 그러나 시뮬레이터에서 실행할 때 제약 조건이 깨졌습니다. 여기 내 코드가있다. 내가 뭘 놓치고 있니?iOS : 시각적 형식 제약이 깨졌습니다.
_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];
'loadView' 또는'viewDidLoad' 또는 다른 곳에서이 작업을 수행하고 있습니까? – Rob
viewDidLoad를 사용하고 있습니다 – LoneWolfPR
그러면 NIB 또는 스토리 보드를 사용한다고 가정하면'self.view'를 대체하지 않고'self.view'의 서브 뷰로지도를 추가하고 적절한 제약 조건을 설정해야합니다 ,뿐만 아니라. 내 수정 된 답변을 참조하십시오. – Rob