簡體   English   中英

將UIButton添加到subView

[英]Adding UIButton to subView

當用戶點擊屏幕時,PopUp應該出現,其中有一個按鈕。 但我不知道為什么按鈕沒有顯示在PopUp中。 是否存在問題,因為它是子視圖中的子視圖?

-(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的坐標是否有可能是錯誤的? 我不確定坐標系是來自主視圖還是來自popUp子視圖。

如果該按鈕將成為子視圖的子視圖,則需要在將包含它的視圖添加到主視圖之前添加按鈕....即您添加按鈕太晚了。

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

按鈕在那里,但由於maskToBounds設置為YES,因此不可見。 嘗試將其設置為NO僅用於測試目的。 然后修復按鈕的x,y坐標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM