gpt4 book ai didi

ios - 键盘出现时移动 subview

转载 作者:行者123 更新时间:2023-12-01 19:05:57 26 4
gpt4 key购买 nike

我正在尝试使用 subview 显示评论列表。我将 subview 添加到 self.view,并添加了一个 tableview 以显示评论列表。现在我想让用户添加评论,我尝试在屏幕底部使用文本字段和按钮向第一个 subview 添加另一个 subview ,但现在我想在键盘出现时将此 View 向上移动然后返回提交评论时的底部,我尝试在 textfieldBeginEditing 时更改评论 View 框架,但它没有改变!我的方法行不通,你有什么想法做我想做的吗?

谢谢

这就是我正在做的,名为 myview 的 View 是我想要向上移动的:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

// notification button
myView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight)];
[myView setBackgroundColor:[UIColor whiteColor]];
[myView setTag:2013];

commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
[commentView setBackgroundColor:[UIColor redColor]];
// [commentView viewWithTag:2031];

table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
table.dataSource=self;
table.delegate=self;

UILabel *currentdate = [[UILabel alloc] initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
currentdate.backgroundColor = [UIColor clearColor];
[currentdate setTextColor: [UIColor blueColor]];
[currentdate setText:@"Comments"];
currentdate.textAlignment= UITextAlignmentCenter;
currentdate.font = [UIFont fontWithName:@"Helvetica" size:20.0];


commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(60,10,screenWidth-60,30)];

commentFeild.placeholder=@"add comment";
commentFeild.backgroundColor = [UIColor whiteColor];
[commentFeild setTextColor: [UIColor blueColor]];
commentFeild.textAlignment= UITextAlignmentRight;
commentFeild.font = [UIFont fontWithName:@"Helvetica" size:15.0];
[commentFeild.layer setCornerRadius:14.0f];
[commentFeild setTag:2113];
//[commentFeild setDelegate:self];

UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
doneBtn.backgroundColor = [ UIColor clearColor];
[doneBtn setTitle:@"done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];

UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(5,10,50,30);
add.backgroundColor = [ UIColor clearColor];
[add setTitle:@"add" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[add addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];

if(![taskObject.status isEqualToString:@"doneTask"])
{
[commentView addSubview:commentFeild];
[commentView addSubview:add];
}

[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];

[self.view addSubview:myView];

最佳答案

你需要在键盘出现和消失时使用通知

viewDidLoad方法添加这个

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil];

当键盘出现和消失时,上述方法会被触发使用它来调整你的 tableview

例如
  -(void)keyboardDidShow:(NSNotification *)notification
{
NSLog(@"KeyBoard appeared");
NSDictionary *info=[notification userInfo];
NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyBoardRect=[aValue CGRectValue];
keyBoardRect=[self.view convertRect:keyBoardRect fromView:nil];
CGFloat keyBoardTop=keyBoardRect.origin.y; //i am getting the height of the keyboard

self.aTableView.contentInset=UIEdgeInsetsMake(0, 0, keyBoardTop+50, 0); //adjust the height by setting the "contentInset"


}

当键盘消失时这样做
  -(void)keyboardDidDisappear:(NSNotification *)notification
{
NSLog(@"KeyBoard Disappeared");

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.addContactTableView.contentInset=UIEdgeInsetsMake(10, 0, 10, 0); //set to normal by setting the "contentInset"

[UIView commitAnimations];

}

解决这个问题你可以满足你的要求

希望这可以帮助你:)

关于ios - 键盘出现时移动 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19629385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com