ios - UIButton 没有添加到 UIView

标签 ios objective-c uiview uibutton

我在这里尝试在viewforprofileandsetting View 上添加两个按钮,但下面的代码不起作用:

( 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. 按钮的框架需要根据父 View 进行设置,因此无需考虑它的框架。

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

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

关于ios - UIButton 没有添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647038/

相关文章:

ios - 大于等于的 NSPredicate 问题

ios - 对于 iOS 8 和 iOS 9 中的某些特定范围,NSMutableAttributedString 不显示不同颜色的字符串

objective-c - iOS9 WKWebview didReceiveAuthenticationChallenge 调用主机为空

iphone - 无法更改 UIView 框架大小

ios - 将数据从 Viewcontroller 传输到 UIview

ios - 启动带有播放列表的 iOS Youtube 应用程序

ios - xcode swift : NSUserDefaults : save the highscore if quit app and restart it where left off

ios - 单击 IOS 后按钮标签颜色更改

ios - 在代码中创建几何基础 View 的正确方法

ios - 在 iOS 中设置按钮文本的颜色