有没有办法以编程方式获得UIBarButtonSystemItem的宽度。对于系统项,width属性始终返回0。具体地说,我希望获得UIViewController的editButtonItem属性的确切宽度。
在iPhone上,这个值是44,但是在iPad上,这个值要大一点,我无法确定它。
发布于 2011-07-14 00:20:21
我从@JoBu1324留下的评论链接中得到了答案。
下面是我使用的代码。
UIBarButtonItem *item = /*...*/;
UIView *view = [item valueForKey:@"view"];
CGFloat width = view? [view frame].size.width : (CGFloat)0.0;发布于 2011-07-13 16:38:53
显然,UIBarButtonItem没有frame属性。因为UIBarButtonItem是NSObject的直接子类(反过来没有框架),所以不可能从代码中获得UIBarButtonItem的框架(文档中的API)。
您必须依靠一些其他方法来计算宽度,因为您在iPhone中发现44是editButtonItem的宽度;-)
发布于 2011-07-13 18:30:44
UIBarButtonItem具有属性width,但对于系统按钮,该属性通常为0。因此,我通常使用自定义按钮并在按钮内部使用本地化字符串,或者使用flexibleSpace UIBarbuttonItem在左右按钮之间添加灵活的空格。字符串的宽度可以通过NSString的-sizeWithFont方法计算出来。通过UIBarButtonItem的UIBarButtonSystemItemFlexibleSpace风格可以获得灵活的空间。
https://stackoverflow.com/questions/6675723
复制相似问题