新しいオプション「ボタン形状」を使用して、iOS 7.1 のコードを修正することにまだ行き詰まっています。UIButtonItem
それを選択すると、プログラムで作成されたツールバーの幅が広がります。テキストフィールドの幅を設定するためにツールバーの(変化する)利用可能なスペースを取得する方法がわからないため、これによりテキストフィールドが右に移動します。
ボタンのサイズを取得しようとしましたが、customItem.customView.frame.size.width);
常に「0.0」が返されます。
テキストフィールドを提供するために、プログラムでボタンとテキストフィールドを備えたツールバーを作成する必要があります。しかし、今までテキストフィールドの幅 (210) をハードコーディングして、ボタンの横にある利用可能なスペースに収まるようにしていました (残念です)。ボタンのサイズをテキスト フィールドのサイズに合わせる方法や、「ボタンの形状」オプションに基づいてテキスト フィールドのサイズを変更する方法が見つかりません。
私に何ができる?ありがとう。
[self.navigationController setToolbarHidden:NO]; //animated:YES
[self.navigationController.toolbar setBarTintColor:[UIColor colorWithRed:0.8f green:0.0f blue:0.0f alpha:0.5]];
[self.navigationController.toolbar setTranslucent:YES];
// button
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"Simulation" style:UIBarButtonItemStylePlain target: self action: @selector(prepareResultView:)];
// text field
CGRect frame = CGRectMake(0, 0, 210, 30);
numberText= [[UITextField alloc] initWithFrame:frame];
numberText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
numberText.borderStyle = UITextBorderStyleRoundedRect;
numberText.textColor = [UIColor blackColor];
numberText.font = [UIFont systemFontOfSize:18.0];
numberText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
numberText.textAlignment = NSTextAlignmentCenter;
numberText.placeholder = NSLocalizedString(@"Anzahl Berechnungen", @"Message");
numberText.backgroundColor = [UIColor whiteColor];
numberText.autocorrectionType = UITextAutocorrectionTypeNo;
numberText.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
numberText.returnKeyType = UIReturnKeyDone;
numberText.clearButtonMode = UITextFieldViewModeWhileEditing;
numberText.delegate = self;
[numberText setRightViewMode:UITextFieldViewModeAlways];
// textfield item
UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc] initWithCustomView:numberText];// behavior of number textfield see below
NSArray *items = [NSArray arrayWithObjects: customItem, customItem1, nil];
[self setToolbarItems:items animated:YES];