簡體   English   中英

UIButton未添加在UIView上

[英]UIButton is not adding on UIView

在這里我試圖在viewforprofileandsetting視圖上添加兩個按鈕,但是下面的代碼不起作用:

( UIView * )viewForProfileAndSettingButton 
{
    setingAndProfileView=[[ UIView alloc ]initWithFrame:CGRectMake(0,_listView.frame.size.height, _listView.frame.size.width, _listView.frame.size.height/4)];
    setingAndProfileView.backgroundColor=[ UIColor darkGrayColor ];
    profileViewButton=[[ UIButton alloc ]init];
    profileViewButton =[ UIButton buttonWithType:UIButtonTypeRoundedRect];
    profileViewButton.backgroundColor=[ UIColor whiteColor ];
    [ profileViewButton setTitle:@" profile View" forState:UIControlStateNormal ];
   profileViewButton.frame=CGRectMake(setingAndProfileView.frame.origin.x+5, setingAndProfileView.frame.origin.y+10, setingAndProfileView.frame.size.width/2-50, setingAndProfileView.frame.size.height/2+10);

    settingButton=[[ UIButton alloc ]init];
    settingButton.backgroundColor=[ UIColor whiteColor ];
    settingButton=[ UIButton buttonWithType:UIButtonTypeRoundedRect ];

    settingButton.frame=CGRectMake(profileViewButton.frame.origin.x+profileViewButton.frame.size.width+10, setingAndProfileView.frame.origin.y+10, setingAndProfileView.frame.size.width/2-50, setingAndProfileView.frame.size.height/2+10);
    [ settingButton setTitle:@" setting View" forState:UIControlStateNormal ];

    [ setingAndProfileView addSubview:profileViewButton ];
    [ setingAndProfileView addSubview:settingButton ];

    return setingAndProfileView;
}

您的代碼存在一些問題:

  1. 您如何宣布這些觀點的? 它們是viewController類的實例變量嗎?
  2. 您只需要調用[[UIButton alloc] init][ UIButton buttonWithType:UIButtonTypeRoundedRect ]
  3. 按鈕的框架需要相對於超級視圖進行設置,因此無需考慮其框架。

這是它的外觀(假設值已經聲明):

-( UIView * )viewForProfileAndSettingButton 
{
    setingAndProfileView=[[ UIView alloc ]initWithFrame:CGRectMake(0,_listView.frame.size.height, _listView.frame.size.width, _listView.frame.size.height/4)];
    setingAndProfileView.backgroundColor=[ UIColor darkGrayColor ];

    profileViewButton =[ UIButton buttonWithType:UIButtonTypeRoundedRect];
    profileViewButton.backgroundColor=[ UIColor whiteColor ];
    [ profileViewButton setTitle:@" profile View" forState:UIControlStateNormal ];
    profileViewButton.frame=CGRectMake(5, 10, setingAndProfileView.frame.size.width/2-50, setingAndProfileView.frame.size.height/2+10);

    settingButton=[ UIButton buttonWithType:UIButtonTypeRoundedRect ];
    settingButton.backgroundColor=[ UIColor whiteColor ];
    settingButton.frame=CGRectMake(profileViewButton.frame.origin.x + profileViewButton.frame.size.width+10, 10, setingAndProfileView.frame.size.width/2-50, setingAndProfileView.frame.size.height/2+10);
    [ settingButton setTitle:@" setting View" forState:UIControlStateNormal ];

    [ setingAndProfileView addSubview:profileViewButton ];
    [ setingAndProfileView addSubview:settingButton ];

    return setingAndProfileView;
}

暫無
暫無

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

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