这样:btn.frame=CGRectMake(x,y,width,height);[btnsetTitle:@"search"forState:UIControlStateNormal];//设置按钮上的自体的大小//[btnsetFont:[UIFontsystemFontSize:14.0]];//这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法//应该使用btn.titleLabel.font=[UIFontsystemFontOfSize:14.0];[btnseBackgroundColor:[UIColorblueColor]];//最后将按钮加入到指定视图superView[superViewaddSubview:btn];扩展资料:注意事项创建按钮的两种方法:1、动态创建btnfont=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];[btnfontsetFrame:CGRectMake(100,10,120,40)];[btnfontaddTarget:selfaction:nilforControlEvents:UIControlEventTouchUpInside];[btnfontsetTitle:@"字体"forState:UIControlStateNormal];btnfont.backgroundColor=[UIColorclearColor];[self.viewaddSubview:btnfont];2、在xib文件中已经创建好,通过tag获取按钮UIButton*testButton=(UIButton*)[self.viewviewWithTag:100];[testButtonaddTarget:selfaction:@selector(test:)forControlEvents:UIControlEventTouchUpInside];
阅读全文 >