简体   繁体   English

在UIScrollView中添加文本字段时,按钮消失

[英]Button disappears when textfield is added in UIScrollView

I am working on an app where a UITextField is added dynamically to a scrollview. 我正在将UITextField动态添加到滚动视图的应用程序上。 For the scrollview I am using the following code: 对于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);
}

There are two buttons and they are placed on the scrollview. 有两个按钮,它们位于滚动视图上。 When I click on the first button, a textfield should be generated between the first button and second button, and the second button should move down. 当我单击第一个按钮时,应在第一个按钮和第二个按钮之间生成一个文本字段,第二个按钮应向下移动。

The problem I am facing: 我面临的问题:

1) When the textfield is dynamically added, the second button disappears from the scrollview 1)动态添加文本字段后,第二个按钮从滚动视图中消失

2) The scrollview does not work after adding the textfield 2)添加文本字段后,滚动视图不起作用

3) How can I change the size of the scrollview when the textfield is added? 3)添加文本框后,如何更改滚动视图的大小?

The code for adding textfield I am using is: 用于添加我正在使用的文本字段的代码是:

// 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];

What am I doing wrong? 我究竟做错了什么?

- (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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM