2013-07-08 51 views
3

當用戶在屏幕上點擊時,彈出窗口應該會顯示一個按鈕。 但我不知道爲什麼按鈕不顯示在PopUp中。是否有問題,因爲它是子視圖中的子視圖?將UIButton添加到子視圖

-(void) popUpWithX:(int)x andY:(int)y { 
    CGRect popUpRect = CGRectMake(x, y, 125, 75); 
    popUp = [[UIView alloc] initWithFrame:popUpRect]; 
    popUp.backgroundColor = [UIColor whiteColor]; 
    popUp.layer.cornerRadius = 7.5f; 
    popUp.layer.masksToBounds = YES; 

    [self.view addSubview:popUp]; 
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; 
    [button setTitle:@"Click me!" forState:UIControlStateNormal]; 
    [popUp addSubview:button]; 
} 

編輯:

是否有可能在UIButton的座標是錯誤的?我不確定座標系是來自主視圖還是來自彈出子視圖。

+0

座標來自彈出窗口,而不是主視圖。這很可能不是問題。 –

回答

2

按鈕存在,但由於maskToBounds設置爲YES而不可見。嘗試它只是爲了測試目的設置爲NO。然後修復按鈕的x,y座標。

+0

是的,現在它工作正常。感謝那! – Sebastian

2

如果該按鈕將成爲子視圖的子視圖,那麼在添加包含它的視圖到主視圖之前,您需要添加該按鈕。你添加按鈕的時間太晚了。

//Move this line to the end of the block 
[self.view addSubview:popUp];//call this after you add your subViews to popUp 
+0

我測試了這個,並沒有解決問題。 –

+0

沒有爲我工作... – Sebastian