Hassy
Hassy

Reputation: 5208

iOS10 - Corner Radius is not working right with border

I've a UIViewController and I'm displaying it as UIModalPresentationPopover. It had its corner radius set to 10.0 and border width to 1.0.

- (void)viewWillLayoutSubviews {
  [super viewWillLayoutSubviews];     

  self.view.layer.borderWidth = 1.0f;
  self.view.layer.cornerRadius = 10.0f;
  self.view.layer.masksToBounds = YES;
}

It is not showing any border along corner radius and it is giving a weird effect. It was working fine before iOS10. What should I do to solve this problem?

Edit: The screenshots are as enter image description here

If i add 2 pixel border then still 1 pixel is missing from View enter image description here

Upvotes: 6

Views: 3263

Answers (3)

Hassy
Hassy

Reputation: 5208

i Solved my Problem by following code

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")){
    self.view.superview.layer.cornerRadius = 15.0f;
}

I did set the view corner radius to 10.0 so 15.0 was exact value at which the superview wont meddle with its subview corner radius and borders to show it as blur or 1 pixel less.

Edit: Custom created UIViewController if animated might need to implement above code after it has finished animating.

Upvotes: 2

Nimit Parekh
Nimit Parekh

Reputation: 16864

New one thing into XCode8 we can't directly set the cornerRadius of the layer.

When you want to apply cornerRadius of UIView need to add one line of code before applying cornerRadius.

yourButton.layoutIfNeeded()

Example into Objective C.

[yourButton layoutIfNeeded];
yourButton.layer.cornerRadius = yourButton.frame.size.height/2;
[[yourButton layer] setBorderWidth:2.0f];

Example into Swift3

self.layoutIfNeeded()
yourButton.layer.cornerRadius = self.frame.height / 2.0
yourButton.layer.borderWidth = 2.0

Upvotes: 4

KAR
KAR

Reputation: 3361

Your code is good but it is missing one line,

view.clipsToBounds = true

Set this line and run again.

Hope this will help you.

Upvotes: 8

Related Questions