繁体   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