问题
I just updated my Xcode ver from 7.3 to 8.0 and some buttons borders disappeared.
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)
now:
回答1:
The reason is that XCode 8 introduces a new way to zoom in Storyboards.
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.
In XCode 8, things are different : due to their new zooming system, even the bounds
are not correct before ViewDidLayoutSubviews (they may exist but with dummy values like 1000 x 1000).
In conclusion :
- 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 inawakeFromNib
(in that case, just addself.layoutIfNeeded
before doing anything frame- or boounds-related, in order to force the cell to recalculate its subviews' frame).
回答2:
I think problem in it:
CGRectGetHeight(self.button.frame) / 2;
When you set corner i think height button don't have value or value to larger border will don't show. You can try change it to
self.button.layer.cornerRadius = 15;
If work, I think you can check your logic and set it when height button get right value.
回答3:
Try this and check:
#import <QuartzCore/QuartzCore.h>
self.button.layer.borderColor = [UIColor whiteColor].CGColor;
self.button.layer.borderWidth = 2;
self.button.layer.cornerRadius = 5; // Change this value on your requirement
self.button.clipsToBounds = YES;
回答4:
To force view frame to be calculated, you can try with layoutIfNeeded. For example for a label in a UITableViewCell :
override func awakeFromNib() {
super.awakeFromNib()
self.layoutIfNeeded()
self.qualityIndexLabel.makeRound()
}
回答5:
use dispatch_after
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.button.layer.borderColor = borderColor.CGColor;
self.button.layer.borderWidth = 2;
self.button.layer.cornerRadius = CGRectGetHeight(self.button.frame) / 2; });
回答6:
the RoundedRect is deprecated use UIButtonTypeSystem instead. see https://developer.apple.com/reference/uikit/uibutton?language=objc for more information.
来源:https://stackoverflow.com/questions/39486910/xcode-8-some-buttons-border-removed