2013-07-02 2 views
1

지도 위에 버튼을 놓고 왼쪽에서 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]; 
+0

'loadView' 또는'viewDidLoad' 또는 다른 곳에서이 작업을 수행하고 있습니까? – Rob

+0

viewDidLoad를 사용하고 있습니다 – LoneWolfPR

+0

그러면 NIB 또는 스토리 보드를 사용한다고 가정하면'self.view'를 대체하지 않고'self.view'의 서브 뷰로지도를 추가하고 적절한 제약 조건을 설정해야합니다 ,뿐만 아니라. 내 수정 된 답변을 참조하십시오. – Rob

답변

1

생각을 몇 :

  1. 내가 NOtranslatesAutoresizingMaskIntoConstraints을 설정할 것입니다.

  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([email protected])]"@"V:[button([email protected])]-10-|".

+0

감사합니다. 나는 위의 시도하고 예외로 끝냈다 :''NSInternalInconsistencyException ', 이유 :'-layoutSubviews를 실행 한 후에도 자동 레이아웃이 필요하다. MKMapView의 -layoutSubviews 구현은 super를 호출해야합니다. " – LoneWolfPR

+0

@LoneWolfPR 당신이'viewDidLoad'에서 이것을한다고 가정하면,'self.view'를 맵에 설정하는 것이 아니라 맵을' self.view', 적절한 제약 조건을 설정하면 오류가 사라집니다. – Rob

+0

지도를 하위보기로 추가하면 오류가 해결되었습니다. 단추가 표시되어야하지만 이제지도가 나타나지 않습니다. 편집 - 죄송합니다, 그냥 새로운 제약을 발견. 그것을 지금 추가 할 것입니다. – LoneWolfPR