2015-04-15 29 views
0

在這裏,我想加上兩粒扣viewforprofileandsetting看法,但下面的代碼無法正常工作:的UIButton不加入上的UIView

(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

在哪裏,你怎麼樣聲明的每個上述意見/按鈕?另外,請解釋上述代碼如何不起作用,如預期的和實際的結果。 – ZeMoon

+0

設置y座標正確。如果將其設置爲0或更小,則查看高度將會很好 –

回答

1

有你的代碼的幾個問題:

  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; 
}