向UIButton添加一个子视图 [英] Adding a subview to UIButton

查看:259
本文介绍了向UIButton添加一个子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将customBadge添加为UIButton的子视图-

I'm trying to add a customBadge as a subview of a UIButton -

到目前为止,这是我的代码-

this is my code so far -

//msg count initiaition
//CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:@"2"];
CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:@"2"
                                               withStringColor:[UIColor whiteColor]
                                                withInsetColor:[UIColor redColor]
                                                withBadgeFrame:YES
                                           withBadgeFrameColor:[UIColor redColor]
                                                     withScale:2.0
                                                   withShining:YES];

    // Set Position of Badge 1
[customBadge1 setFrame:CGRectMake(self.view.frame.size.width/2-customBadge1.frame.size.width/2+_MsgHeadBtn.frame.size.width/2, 110, customBadge1.frame.size.width, customBadge1.frame.size.height)];
 //add badge to view
[_MsgHeadBtn addSubview:customBadge1];

我要向其添加子视图的按钮是_MsgHeadBtn,这是下面屏幕截图顶部LH的电子邮件图标. 我试图使自定义徽章略微出现在电子邮件图标的右上方-但最终在屏幕截图中显示了结果!

The button I'm trying to add the subview to is _MsgHeadBtn, which is the email icon on top LH of the screenshot below. I was trying to make the custom badge appear slighty above and to the right of the email icon - but I end up with the result in the screenshot!

任何人都可以提供有关我要去哪里的建议吗??

Can anyone offer any advice as to where i'm going wrong!?

推荐答案

问题在您的setFrame:方法中.您正在使用self.view.frame.size.width.

Issue is within your setFrame: method. You are using self.view.frame.size.width.

检查以下代码:

[customBadge1 setCenter:CGPointMake(_MsgHeadBtn.frame.size.width, 0)];
[_MsgHeadBtn addSubview:customBadge1];

[customBadge1 setFrame:CGRectMake(_MsgHeadBtn.frame.size.width, 0, customBadge1.frame.size.width, customBadge1.frame.size.height)];
[_MsgHeadBtn addSubview:customBadge1];

这篇关于向UIButton添加一个子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆