我想添加UIButton
。但是,我需要显示以下项目。
首先,我需要显示一些文本,然后是小图像,然后再显示一些文本,然后是图像,如上图所示。我怎么能这样做?
另外,我需要知道这是否符合苹果UI指南。如果没有,请建议我怎么做这个? (可以改为使用标签)
代码
button= [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake(210, 285, 100, 18)];
//[button setTitle:@"Show View" forState:UIControlStateNormal];
[button
addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
答案 0 :(得分:2)
我建议你创建自定义UIButton,然后在按钮上添加UILabels和UIImageViews。
请注意,您可以根据需要向按钮添加尽可能多的元素,因为UIButton是UIView的子类(这可能只通过代码,而不是通过Interface Builder)。
另一种选择是创建自定义UIView,然后添加这些元素以及手势以收集按钮等等。
此外,如果此按钮仅为一次,则可以在视图控制器中执行此操作。
要向UIButton添加任何元素,您只需:
[myButton addSubview:particularSubview]
如果您有三个标签和两个图像,请重复此过程五次。
请记住,您应该使用框架初始化UI元素,因此它们不会重叠:
UILabel *ll = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
第一个参数是x,第二个是y,第三个是宽度,第四个是高度。
答案 1 :(得分:0)
不确定是否可以使用本机UIButton
执行此操作,但您可以继承UIView
并在此视图中创建元素。
然后只需覆盖- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
方法以检测何时按下整个视图(也就是按钮)
正如其他答案所示,你也可以将UIButton子类化(给你点击手势oob)
答案 2 :(得分:0)
您可以使用Apple,IBDesignable和IBInspectacle属性引入的新功能(我写了basic tutorial here但您可以找到大量样本on GitHub)。
作为一个起点,将UIButton子类化为您自己的类,并手动添加您需要的组件。
答案 3 :(得分:0)
使用图像创建按钮的最简单方法是
-(void)createButton:(NSString *)text atPos:(CGPoint)pos{
UIImage *image=[UIImage imageNamed:@"image.png"];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:text forState:UIControlStateNormal];
//it is important to use image size same for the frame if you are setting image in background mode..
[button setFrame:CGRectMake(pos.x, pos.y, image.size.width, image.size.height)];
[self.view addSubview:button];
}
然后将其调用到您想要定位的位置。
[self createButton:@"Text" atPos:CGPointMake(0, 0)];
但是,您也可以尝试为解决方案设置edge inset
请参阅here以获取一些想法。
希望它有所帮助。
干杯。