我正在学习iOS。我使用Xcode 4.3.2,我创建了一个按钮,在回调中,我使用了以下代码。
1)创建单视图应用程序 2)创建新视图 - > newV 3)创建按钮但是; 4)添加但作为newV的子视图。 5)添加newV作为主视图的子视图。
-(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,但是没有显示按钮。如何解决这个问题?
答案 0 :(得分:0)
在添加视图newV后尝试添加按钮。
答案 1 :(得分:0)
创建按钮时,您没有提供框架。尝试使用:
UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
创建按钮时,它的大小为零,因此您无法看到它。