Xcode 8 - 删除了一些按钮边框 [英] Xcode 8 - Some buttons border removed

查看:200
本文介绍了Xcode 8 - 删除了一些按钮边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的Xcode版本从7.3更新到8.0并且一些按钮边框消失了。

I just updated my Xcode ver from 7.3 to 8.0 and some buttons borders disappeared.

代码看起来很好,所以我真的不知道发生了什么层。
btw - 在其他一些控制器中,我可以看到图层边框。

The code looks fine, so I really don't know what happened to the layers. btw - in some other controllers I can see the layers borders.

self.button.layer.borderColor = borderColor.CGColor;
self.button.layer.borderWidth = 2;
self.button.layer.cornerRadius = CGRectGetHeight(self.button.frame) / 2;

之前:(图片只是例如 - 边框实时看起来不同)

before: (The image is only for example - the borders looks different at real time)

现在:

推荐答案

原因是XCode 8引入了一种新的放大方式故事板。

The reason is that XCode 8 introduces a new way to zoom in Storyboards.

在XCode 8之前,在视图控制器生命周期中, frames 在viewDidLoad中是未知的(或者在属性 didSet )。您必须等到viewDidLayoutSubviews(这是Autolayout完成应用约束以确定主视图中每个子视图的帧的时间。
但是 bounds 之前可以访问那:它们只是设置为故事板中IBOutlet的大小。

Before XCode 8, in the view controller life cycle, frames were not known in viewDidLoad (or in properties didSet). You had to wait until viewDidLayoutSubviews (which is when Autolayout had finished applying the constraints to determine the frames of every subview in the main view. But bounds were accessible before that: they were just set to the size of the IBOutlet in the storyboard.

在XCode 8中,情况有所不同:由于他们新的缩放系统,甚至<$在ViewDidLayoutSubviews之前c $ c> bounds 是不正确的(它们可能存在,但虚拟值如1000 x 1000)。

In XCode 8, things are different : due to their new zooming system, even the boundsare not correct before ViewDidLayoutSubviews (they may exist but with dummy values like 1000 x 1000).

结论:


  • 你可以在viewDidLoad或IBOutlet
    didSet中使用cornerRadius这样的东西,只要你使用修复值

  • 如果你需要根据 bounds 定义你的cornerRadius,那么在viewDidLayoutSubviews中这样做,或者使用 NSLayoutConstraints (它们的值是固定的并且可以从Autolayout中获知)

  • 如果您需要在视图中使用cornerRadius s (如UITableViewCell或UICollectionViewCell子类),然后您可以在 layoutSubviews 中执行此操作(但是您需要提供固定值或NSLayoutConstraint常量cornerRadius),或者在 awakeFromNib (在这种情况下,只需添加 self.layoutIfNeeded ,然后再做任何框架或发布 - 相关,以强制单元格重新计算其子视图的框架。

  • you can use such things as cornerRadius in viewDidLoad or in the IBOutlet didSet, as long as you use a fixed value
  • if you need to define your cornerRadius based on bounds, then do so in viewDidLayoutSubviews, or use NSLayoutConstraints (their value is fixed and known from Autolayout)
  • if you need to use cornerRadius in views (like UITableViewCell or UICollectionViewCell subclasses), then you can either do so in layoutSubviews (but then you need to give either a fixed value or a NSLayoutConstraint constant to cornerRadius), or in awakeFromNib(in that case, just add self.layoutIfNeeded before doing anything frame- or boounds-related, in order to force the cell to recalculate its subviews' frame).

这篇关于Xcode 8 - 删除了一些按钮边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆