1

i am trying to show comments list by using subview. I added subview to self.view, and i added a tableview to it in order to show comments list. now i want to enable user to add comment, i tried to add another subview to the first one with text field and button, but the view shown up and the text field and button do not. this is the code i use to do so:

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)];
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: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];
4

2 回答 2

1

您正在commentView 中添加commentFeild。

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

commentView 的高度是 70,commentField 的 y 位置是 screenHeight-50,与 commentView 高度相比,这个 y 位置要大。

子视图的 x 和 y 位置是根据父 x 和 y 计算的。

于 2013-10-28T05:40:06.633 回答
0

的框架commentField导致问题,因为 y 值非常大,评论文本字段未显示在评论视图中

纠正框架,它会出现

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

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

于 2013-10-28T05:54:59.417 回答