将subView添加到另一个SubView ios

时间:2013-10-28 00:18:34

标签: ios objective-c uiview

我正在尝试使用子视图显示评论列表。我将subview添加到self.view,然后我添加了一个tableview以显示注释列表。现在我想让用户添加评论,我试图用文本字段和按钮添加另一个子视图到第一个子视图,但是显示的视图和文本字段和按钮没有。这是我用来执行此操作的代码:

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

2 个答案:

答案 0 :(得分: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计算的。

答案 1 :(得分:0)

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

纠正框架,它将显示

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

注意:视图的frame仅取决于其父视图而不是父视图的任何超级视图。在您的情况下,commentField's帧取决于注释视图维度,与任何其他超级视图无关< / p>