我仍然坚持使用新选项"按钮形状"修复我的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];