我正在开发一个应用程序,其中UITextField动态添加到scrollview。对于scrollview,我使用以下代码:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height + kbSize.height);
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height);
}
有两个按钮,它们放在滚动视图上。当我点击第一个按钮时,应该在第一个按钮和第二个按钮之间生成一个文本字段,第二个按钮应该向下移动。
我面临的问题:
1)动态添加文本字段时,第二个按钮将从滚动视图
中消失2)添加文本字段
后,scrollview不起作用3)如何在添加文本字段时更改滚动视图的大小?
我正在使用的添加文本字段的代码是:
// below is called every time first button is clicked
// for moving the second button down
CGRect frame;
frame = self.createButton.frame;
frame.origin.y = 269 + Y;
self.createButton.frame = frame;
//adding textfield dynamically
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 156+Y, 280, 30)];
Y = Y+38;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:14];
textField.placeholder = self.keyField.text;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.scrollView addSubview:textField];
我做错了什么?
答案 0 :(得分:0)
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect frame = self.createButton.frame;
self.scrollView.contentSize = CGSizeMake(0, frame.origin.y + frame.size.height + kbSize.height);
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
CGRect frame = self.createButton.frame;
self.scrollView.contentSize = CGSizeMake(0, frame.origin.y + frame.size.height);
}