2

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

0 回答 0