UIToolbar にカスタム ボタンを追加するために、IB ではなくコードで autolayout を使用しています。私の質問はベストプラクティスについてです。次のコードを使用して、ツールバー内のボタンを追加、設定、およびサイズ変更しますか?
(1)
//-------------------------------------------------------
// Toobar View
//-------------------------------------------------------
UIToolbar *toolbar = [UIToolbar new];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:@"add_normal"] forState:UIControlStateNormal];
[addButton setImage:[UIImage imageNamed:@"add_highlighted"] forState:UIControlStateHighlighted];
[addButton addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addNewItemButton = [[UIBarButtonItem new] initWithCustomView:addButton];
[toolbar setItems:[NSArray arrayWithObject:addNewItemButton] animated:NO];
// Add Views to superview
[superview addSubview:topbarView];
[superview addSubview:_tableView];
[superview addSubview:toolbar];
[toolbar addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-(10)-[addButton(39)]"
options:0
metrics:nil
views:viewsDictionary]];
[toolbar addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-(7)-[addButton(29)]"
options:0
metrics:nil
views:viewsDictionary]];
(2) または、次のような別のコードを使用してボタンのサイズを変更し、フレーム サイズを設定します。
CGRect buttonFrame = addButton.frame;
buttonFrame.size = CGSizeMake(19, 19);
addButton.frame = buttonFrame;
では、ナンバー1が推奨される方法ですか?自動レイアウトの世界では、フレームの設定は普通のことではないと読んだことがありますか?