- (void)viewDidLoad{
int leftBorder = 80;
int topBorder = 160;
int width = 150;
int height = 50;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(leftBorder, topBorder, width, height)];
myView.layer.cornerRadius = 5;
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = CGRectMake(0, 0, 50, 50);
[testButton setTitle:@"testButton" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:self.testButton];
self.myView.hidden = YES;
[super viewDidLoad];
}
嗨,對不起,愚蠢的問題!我是xcode中的newby。爲什麼我沒有看到這個按鈕?如何在點擊後隱藏按鈕?我需要框架內的按鈕。看不到按鈕
你加入你的按鈕'myView'然後躲在'myView'這包含它內的按鈕。 –
你不會看到它,因爲你隱藏了視圖:self.myView.hidden = YES; – tadasz
另一種評論:你使用'int'來構造'CGRect',你應該使用'CGFloat'來代替。 – Pascal