Adding a subview to UIButton(向 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,它是下面屏幕截图左上方的电子邮件图标.我试图让自定义徽章略微显示在电子邮件图标的上方和右侧 - 但我最终得到了屏幕截图中的结果!
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 添加子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 UIButton 添加子视图
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01