本文介绍了UIButton titleLabel帧大小返回CGSize,零宽度&高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于iOS7,我使用 UIButton titleLabel.frame.size.width 属性来确定我的宽度不同本地化下的按钮标题,所以我可以在 UIButton 上使用 contentInset 正确定位标题。 我的 UIButton 设置有图像和标题,我总是希望这两者的组合在 UIButton 例如: [space-x(image)space-y(titleLabel) space-x] 在iOS 7和Xcode 5.1下,以下代码完美运行(即使在iOS上运行) 8 GM在XCode 5.1中构建时:) CGSize buttonSize = button.frame.size; CGSize titleSize = button.titleLabel.frame.size; float contentInset =((buttonSize.width - (titleSize.width + 18 + 3))/ 2); float contentInsetRounded = roundf(contentInset); [button setContentEdgeInsets:(UIEdgeInsetsMake(0,contentInsetRounded,0,0))]; (18是图像的宽度,3是两者之间的点间距数图片和上面示例中的 titleLabel 或space-y) 在iOS 8和Xcode下6 GM, button.titleLabel.frame.size 返回 CGSize ,零宽度&高度所以我的contentInset最终将 UIBmage 放在 UIButton 中,导致 titleLabel 截断。 任何想法?我尝试在此代码之前立即设置 titleLabel 文本,以防它认为它是空的但也没有帮助。 提前致谢!解决方案我已解决它。应用程序在iOS8上运行,由Xcode 6构建,而不是在UIButton setTitle , setTitleEdgeInsets 之后立即更新框架。它将等待下一次UI更新。因此,如果你获得了titleLabel的框架,你将获得CGRectZero。 解决方案: 在设置UI属性后调用以下方法,立即更新布局: [self setNeedsLayout]; [self layoutIfNeeded]; 或者: 延迟一秒,你可以得到 titleFrame ,使用 self.titleLabel.frame 。 或者: 主队列中的dispatch_async,将代码移动到下一个队列 For iOS7, I used the UIButton titleLabel.frame.size.width property to determine the width of my button title under different localisations so I could position the title correctly using contentInset on the UIButton.My UIButton is setup with an image and title and I always want the combination of these two to be centred horizontally in the UIButton for example:[space-x (image) space-y (titleLabel) space-x]Under iOS 7 and Xcode 5.1, the following code works perfectly (even running on iOS 8 GM when built in XCode 5.1):CGSize buttonSize = button.frame.size;CGSize titleSize = button.titleLabel.frame.size;float contentInset =((buttonSize.width - (titleSize.width + 18 + 3)) / 2 );float contentInsetRounded = roundf(contentInset);[button setContentEdgeInsets:(UIEdgeInsetsMake(0, contentInsetRounded, 0, 0))];(18 is the width of the image, and 3 is the number of points spacing between the image and titleLabel or space-y in my example above)Under iOS 8 and Xcode 6 GM, button.titleLabel.frame.size is returning a CGSize with zero width & height so my contentInset ends up centring the UIImage in the UIButton, causing the titleLabel to truncate.Any ideas? I tried setting the titleLabel text immediately before this code in case it thought it was empty but that didn't help either.Thanks in advance! 解决方案 I resolved it. App run on iOS8, build by Xcode 6, not update frame right after UIButton setTitle, setTitleEdgeInsets. It will wait for next UI update. So, if you get frame of titleLabel, you will get CGRectZero.Solution:Call below methods after set UI property, to update layout immediately:[self setNeedsLayout];[self layoutIfNeeded];Or:Delay a second, and you can get titleFrame, use self.titleLabel.frame.Or:dispatch_async in main queue, to move code to next queue 这篇关于UIButton titleLabel帧大小返回CGSize,零宽度&高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 04:27