2013-10-28 171 views
1

我想通过使用子视图显示注释列表。我添加了subview到self.view,并且我添加了一个tableview来显示评论列表。现在我想让用户添加评论,我试图添加另一个子视图到第一个带有文本字段和按钮的子视图,但是显示的视图和文本框和按钮都没有。这是我用来这样做的代码:将子视图添加到另一个子视图ios

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

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

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

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(50,screenHeight-50,screenWidth-50,50)]; 
[email protected]"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:2030]; 
//[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(0,screenHeight-50,40,50); 
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]; 

回答

0

为y值是非常大的注释文本框不显示在评论查看

正确的框架,它会显示

commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(50,0,screenWidth-50,50)]; 

注意commentField的框架是造成问题的原因: frame的视图仅取决于其父视图而不是父视图的任何超级,在您的情况下commentField's框架取决于评论视图尺寸并与其他任何超级视图无关

+0

是的,谢谢。 – etab

+0

你知道如何移动commentView当键盘显示为了写评论,然后在提交评论后退后? – etab

+0

请参阅(此)[https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html] –

1

您正在commentView中添加commentFeild。

commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)]; 

评论查看的高度是70和commentField的y位置是screenHeight-50这y位置是大比较评论查看高度。

子视图的x和y位置是从父母x和y计算出来的。

相关问题