UIButton 未添加到 UIView

Posted

技术标签:

【中文标题】UIButton 未添加到 UIView【英文标题】:UIButton is not adding on UIView 【发布时间】:2015-04-15 09:52:37 【问题描述】:

我正在尝试在 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;

【问题讨论】:

您在哪里以及如何声明上述每个视图/按钮?另外,请解释上述代码是如何不起作用的,如预期和实际结果是什么。 设置 y 坐标正确。如果您将其设置为 0 或更低,则查看高度会很好 【参考方案1】:

您的代码存在一些问题:

    您是如何声明这些观点的?它们是 viewController 类的实例变量吗? 您只需致电[[UIButton alloc] init][ UIButton buttonWithType:UIButtonTypeRoundedRect ]。 按钮的框架需要根据超级视图进行设置,因此无需考虑它的框架。

它应该是这样的(假设已经声明了值):

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

【讨论】:

以上是关于UIButton 未添加到 UIView的主要内容,如果未能解决你的问题,请参考以下文章

在 UIView 中将 Traget 添加到 UIButton。收到错误“以 NSException 类型的未捕获异常终止”

uibutton 未显示

以编程方式将 IBAction 添加到 UIButton

未调用编程 UIButton 操作

动画期间未调用 UIButton 的选择器方法

NSLayoutConstraint 后子视图中的 UIButton 未收到触摸