uibutton *btntemp = [uibuttonbuttonwithtype:uibuttontypecustom]; 深度定制一个按钮 uibutton *btn1 = [uibutton buttonwithtype:uibuttontypecustom]; btn1.frame = cgrectmake(0, 0, 200, 140); btn1.center = cgpointmake(180, 215); btn1.autoresizingmask = uiviewautoresizingflexibletopmargin | uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblerightmargin; [btn1 settitle:@"" forstate:uicontrolstatenormal]; [btn1 addtarget:self action:@selector(btntest) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn1];
疑问 & 发现 两种不同的创建方式,其中一种button 可以显示titile,但是另外一种不能显示。如下所示;
// uibutton *button = [uibutton buttonwithtype:uibuttontyperoundedrect]; //使用这种创建方式,button 不能显示 titile // button.frame = cgrectmake(10+(screen_width*schemecount)+30+100, 20+(j*30), 130, 40);
找到狗血的原因了!使用如下方法创建button时,不能使用[button release]; 否则,button 不能显示标题 uibutton *button = buttonwithtype:uibuttontyperoundedrect];
疑问:经过实践发现,当把一个button 依附到一个 imageview时,button按钮没有按下去的效果,而且,不能够设置背景图片。只能设置颜色。下面代码,在[imageview addsubview:button]; 不起作用。为什么? 难道button 只能add到view上?而不是imageview上?(是不是也因为狗血的 release 啊) [button setbackgroundimage:[uiimage imagenamed:@"button_anpai.png"] forstate:uicontrolstatenormal];
设置uibutton的文字显示位置、字体的大小、字体的颜色 btn.frame = cgrectmake(x, y, width, height); [btn settitle: @"search" forstate: uicontrolstatenormal]; //设置按钮上的自体的大小 //[btn setfont: [uifont systemfontsize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的sdk版本中去除改方法 //应该使用 btn.titlelabel.font = [uifont systemfontofsize: 14.0]; [btn sebackgroundcolor: [uicolor bluecolor]]; //最后将按钮加入到指定视图superview [superview addsubview: btn]; //为什么要指定到superview? ========================================================== tvnamelabel=[[uibutton alloc]initwithframe:cgrectmake(5,5,200,40)]; 这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的, btn.contenthorizontalalignment=uicontrolcontenthorizontalalignmentleft ;//设置文字位置,现设为居左,默认的是居中 但是问题又出来,此时文字会紧贴到做边框,我们可以设置 btn.contentedgeinsets = uiedgeinsetsmake(0,10, 0, 0); 使文字距离做边框保持10个像素的距离。 ======================================================= 设置uibutton上字体的颜色设置uibutton上字体的颜色,不是用: [btn.titlelabel settextcolor:[uicolorblackcolor]]; btn.titlelabel.textcolor=[uicolor redcolor]; 而是用: [btn settitlecolor:[uicolor blackcolor]forstate:uicontrolstatenormal];
长按按钮响应事件
{ //长按删除按钮 uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc]initwithtarget:self action:@selector(deletelongpressed)]; // longpress.minimumpressduration = 0.5; //最短按压时间,一般地,此句不
uibutton 两种设置背景图片方式的区别 第一种:setbackgroudimage :图片被拉伸 第二种:setimage : 图片保持原大小
参考:http://my.oschina.net/joanfen/blog/160843 讲图片进行拉伸设置为button的背景。
非alloc 的button 都不要release ,否则button 显示不出来。虽然button的 retaincount 还是1 。如下 //button 剧集 button = [uibutton buttonwithtype:uibuttontypecustom]; button.frame = cgrectmake(0, k_headimageview_height, screen_width/2-1, 84/2-1); [button setbackgroundimage:[imageutilities createimagewithcolor:[uicolor whitecolor]] forstate:uicontrolstatenormal]; [button setbackgroundimage:[imageutilities createimagewithcolor:[colorutils colorwithhexstring:k_buttonselect_backgroudcolor]] forstate:uicontrolstateselected]; [button settitle:@"剧集" forstate:uicontrolstatenormal]; [button.titlelabel setfont:[uifont systemfontofsize:k_introduceinfo_fontsize]]; [button settitlecolor:[colorutils colorwithhexstring:k_introduceinfo_textcolor] forstate:uicontrolstatenormal]; [button settitlecolor:[colorutils colorwithhexstring:k_buttonselect_textcolor] forstate:uicontrolstateselected]; [button addtarget:self action:@selector(btnepIphone 苹果 ios declicked:) forcontrolevents:uicontroleventtouchupinside]; epIphone 苹果 ios debutton = button; [self.view addsubview:epIphone 苹果 ios debutton]; //[button release]; // 若此处添加 release, button 将显示不出来。
如何设置能让uibutton点一下,保持按下去的状态,再点一下弹起来?方法:做一个判断修改selected属性如: [_tmpbtn setselected:yes]; or [_tmpbtn setselected:no];
uibutton 的的对象 button 有三种 状态,分贝是normal selected highlighted ,他们之间有什么区别? uicontrolstatenormal 指按钮的正常状态,这个没有异议吧。 uicontrolstatehighlighted 指按钮被按下去时的状态! uicontrolstateselected 指按钮被选中时的状态! “按下去”和“选中”有什么区别?“按下去时状态”指被手指按压时的状态。“选中时状态”指手指按压已经完毕,并离开。按钮呈现的状态。所以,设置一个按钮被按下去时的图片变化时,要用uicontrolstatehighlighted而不是uicontrolstateselected。 参考:关于uibutton的highlighted状态的总结 uibutton的一个小陷阱——注意:titlelabel是readonly的!在使用uibutton的时候,有时需要在运行时动态改变按钮的文字、样式。但ios sdk的文档说得不是很清楚,使用错误的api经常会产生诡异的结果,如点击uibutton后,text变回默认值。 这问题我遇到过三回了,每回都要重新google一下才解决。以前觉得这些不起眼的 此文来自: 马开东博客 转载请注明出处 网址: http://www. 小问题,不值得单独写一篇文章,但往往这些问题现常见。以后会持续把遇到的一些小问题作为tips的形式分享出来,希望也可以帮助到其它的开发者。
错误的方式
这里titlelabel是readonly的,但我们可以改变titlelabel的property。问题出在uibutton是有 正确的方式
如果其它状态没有特别设置的话,设置 参考: http://www./2012/09/05/caveat-for-uibutton/
让uibutton在按下时没有高亮效果方法一: button1.adjustsimagewhenhighlighted = no; //取消按钮高亮状态(有些时候,我们并不想要系统给的那个状态) 参考:http://www./bbs/simple/?t106229.html 方法二:为uibutton所有状态设置一张透明的图片。详情请看:让uibutton在按下时没有高亮效果 设置按钮被点中的高亮光晕效果[cancelbutton setshowstouchwhenhighlighted:yes];
按钮扩大触摸响应区域 原来代码如下:menubtn.frame = cgrectmake(4, 8, 44, 28); 扩大触摸区域代码如下: [menubtn setimage:[pyutiles imagefromfile:@"menubtn.png"] forstate:uicontrolstatenormal]; [menubtn setframe:cgrectmake(0, 0, 48, 44)]; [menubtn setcontentmode:uiviewcontentmodecenter]; 原理,扩大button的frame rect,并且将图片设置成居中即可 注意,这边要是setimage,setbackgroundimage不行,会扩大图片到整个响应区域 参考:http://www./mobile/550219.html 设置uibutton 的字体阴影 [[rightbutton titlelabel] setshadowcolor:[uicolor blackcolor]]; [[rightbutton titlelabel] setshadowoffset:cgsizemake(-0.5, -0.5)];
调整button内部内容的边距(padding) [self.usernamebutton setcontentedgeinsets:uiedgeinsetsmake(0, 3, 0, 0)]; 参考:http://blog.马开东/ysy441088327/article/details/7660183 保持uibutton高亮状态 (点击进入) 参考:关于uibutton的highlighted状态的总结 (在按钮up的时候button的highlighted状态会被clear,)
关于uibutton选中和高亮状态下的图片切换 (点击进入) 总结:当button的状态为selected的时候,高亮时对应的状态应该是selected|highlighted,这与normal下高亮的状态highlighted不同。 代码如下: [mybutton setimage:imageselectedhover forstate:(uicontrolstateselected | uicontrolstatehighlighted)];
uibuttontypecustom 和 uibuttontyperoundedrect 的区别 uibuttontypecustom :是一个背景透明的按钮。 如果想做按钮中图片切换(不同状态),必须使用两张图片进行设置。 uibuttontyperoundedrect :是一个圆角矩形按钮。方便之处:系统会自动为这种按钮设置高亮状态(半透明状态)。即,通过一张图片,就可以做出按钮交互的效果。 不方便的地方:如果你想自定义不同状态下,按钮显示的背景,这种类型的按钮就支持的不好。 建议:快速开发,每个按钮只有一张图片,建议使用uibuttontyperoundedrect。按钮高度自定义,建议使用uibuttontypecustom。
titleedgeinsets imageedgeinsets 的使用 [mybutton settitleedgeinsets:uiedgeinsetsmake(0, 0, 0, 0)]; 4个参数是上边界,左边界,下边界,右边界。
使用uibutton和uiimageview时的坑 -》使用将uibutton设置为透明,然后使用uiimage 或者uiimageview作为uibutton的父view,可能会使uimage覆盖的那片uibutton区域失去事件响应。解决方案 :设置uiimage或者uiimageview的人机交互属性为yes. ***.userinteractionenabled = yes; 2、同理,如果将一个imageview作为uib
此文来自: 马开东博客 网址:http://www. 站长QQ
|
|