Objective c 将新视图添加到另一个子视图中
我正在学习iOS。我使用的是Xcode 4.3.2,我创建了一个按钮,在回调中,我使用了以下代码 1) 创建了单视图应用程序 2) 创建新视图->新建 3) 创建一个按钮,但是; 4) 添加但作为newV的子视图。 5) 将newV添加为主视图的子视图Objective c 将新视图添加到另一个子视图中,objective-c,ios4,Objective C,Ios4,我正在学习iOS。我使用的是Xcode 4.3.2,我创建了一个按钮,在回调中,我使用了以下代码 1) 创建了单视图应用程序 2) 创建新视图->新建 3) 创建一个按钮,但是; 4) 添加但作为newV的子视图。 5) 将newV添加为主视图的子视图 -(IBAction) submitButtonPressed:(id)sender { NSLog (@" Submit Button is pressed "); UIView *newV = [[UIView alloc]
-(IBAction) submitButtonPressed:(id)sender
{
NSLog (@" Submit Button is pressed ");
UIView *newV = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
newV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIButton *but = [[UIButton alloc] init];
[but setTitle:@"SubView" forState:UIControlStateNormal];
[newV addSubview:but];
[self.view addSubview:newV];
}
但我遇到的问题是,只显示视图viewV,而不显示按钮。如何解决这个问题 在视图newV中添加按钮后尝试添加按钮。创建按钮时,未提供框架。尝试使用:
UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
当您创建按钮时,它的大小为零,因此您无法看到它。我按照您的建议进行了更改,但仍然无法看到按钮。抱歉,我误解了问题,我认为按钮是主视图的一部分。我注意到按钮没有框架。只需使用initWithFrame而不是init for UIButton,它就会显示在您的视图中。