Я создал программный контроллер простого вида с помощью двух кнопок на нем. Если я поеду в старую школу и задаю размеры рамки для макета, тогда все будет хорошо. Однако, если я использую ограничения макета для макета, то кнопки отображаются отлично, но они не реагируют на нажатия. Они даже не выделяют. Мой метод loadview приведен ниже в форме, которая не позволяет кнопкам работать. Если код установки кадра раскоментирован, а добавление ограничений добавлено, то кнопки начинают реагировать на нажатия, как ожидалось. Кто-нибудь знает, что происходит? Я хотел бы перевести весь старый жесткий код в моей базе кода, чтобы быть основаны на ограничениях, но, похоже, приходится сталкиваться с первым препятствием.UIButton перестает работать при использовании автоматической компоновки
- (void)loadView {
self.view = [UIView new];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
self.navigationItem.title = @"Landing Page";
UIButton *buildExercisesButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buildExercisesButton = buildExercisesButton;
// buildExercisesButton.frame = CGRectMake(20, 312, 164, 44);
self.buildExercisesButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.buildExercisesButton setTitle:@"Build Exercises" forState:UIControlStateNormal];
[self.buildExercisesButton addTarget:self action:@selector(buildExercisesButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.buildExercisesButton];
UIButton *organiseExercisesButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.organiseExercisesButton = organiseExercisesButton;
// organiseExercisesButton.frame = CGRectMake(192, 312, 164, 44);
self.organiseExercisesButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.organiseExercisesButton setTitle:@"Organise Exercises" forState:UIControlStateNormal];
[self.organiseExercisesButton addTarget:self action:@selector(organiseExercisesButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.organiseExercisesButton];
NSDictionary *variables = NSDictionaryOfVariableBindings(buildExercisesButton, organiseExercisesButton);
NSArray *constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"|-[buildExercisesButton(organiseExercisesButton)]-[organiseExercisesButton]-|"
options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
metrics:nil
views:variables];
[self.view addConstraints:constraints];
constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[buildExercisesButton]-|"
options:0
metrics:nil
views:variables];
[self.view addConstraints:constraints];
}
In viewDidAppear Я распечатываю рамки кнопок. Они дают странные ценности, которые я не знаю, связано это или нет. На самом деле кнопки отображаются рядом друг с другом в центре экрана.
buildExercisesButton: {{20, -63}, {164, 44}}
organiseExercisesButton: {{192, -63}, {164, 44}}
Хм ... когда я скопировал ваш код, он работал хорошо. Мои журналы были: build frame is: {{20, 397}, {136, 44}} организовать фрейм: {{164, 397}, {136, 44}} – rdelmar