【发布时间】:2014-08-20 19:47:25
【问题描述】:
我正在开发一个将 UITextField 动态添加到滚动视图的应用程序。对于滚动视图,我使用以下代码:
- (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)动态添加textfield时,第二个按钮从scrollview中消失
2)添加文本字段后滚动视图不起作用
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];
我做错了什么?
【问题讨论】:
-
这里最初的Y值是多少?frame.origin.y = 269 + Y;
-
尝试将滚动视图的 contentHeight 添加到第二个按钮 y 位置 + 高度
-
你能通过编辑上面的代码告诉我吗?
-
你是否开启了自动布局?
-
您好,我已经在更改后添加了代码。请立即查看
标签: ios objective-c ios7 uiscrollview