Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I draw a rotated UIView with a shadow but without the shadow being rotated as well
    primarykey
    data
    text
    <p>I'd like the shadow applied correctly after rotation. This is my code:</p> <pre><code>myButton.transform = CGAffineTransformMakeRotation(M_PI / 180.0 * 90.0); myButton.layer.shadowOffset = CGSizeMake(12.0, 12.0); myButton.layer.shadowRadius = 2.0; myButton.layer.shadowOpacity = 0.8; myButton.layer.shadowColor = [UIColor blackColor].CGColor; </code></pre> <p>Without rotation the shadow looks fine:</p> <p><img src="https://i.stack.imgur.com/z2RRW.png" alt="enter image description here"></p> <p>But after rorating it by 90° the shadow is rotated as well:</p> <p><img src="https://i.stack.imgur.com/hYlWV.png" alt="enter image description here"></p> <p>Is there anything I can do about it without overriding the drawRect method and do low level drawing? Or maybe some method which corrects the shadowOffset with a given rotation angle? It's easy to correct the offset by hand for 90° so this is no option ;)</p> <p>It should look like this:</p> <p><img src="https://i.stack.imgur.com/jTRIX.png" alt="enter image description here"></p> <p>Thanks in advance!</p> <p><strong>With help of Bartosz Ciechanowski this works now!</strong></p> <pre><code>float angleInRadians = M_PI / 180.0 * -35.0; myButton.transform = CGAffineTransformMakeRotation(angleInRadians); myButton.layer.shadowOffset = [self correctedShadowOffsetForRotatedViewWithAngle:(angleInRadians) andInitialShadowOffset:CGSizeMake(12.0, 12.0)]; myButton.layer.shadowRadius = 2.0; myButton.layer.shadowOpacity = 0.8; myButton.layer.shadowColor = [UIColor blackColor].CGColor; </code></pre> <p>This results in:</p> <p><img src="https://i.stack.imgur.com/39oBT.png" alt="enter image description here"></p> <p>instead of </p> <p><img src="https://i.stack.imgur.com/Je9tv.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload