With iOS 11 we now have CACornerMask
and the maskedCorners
property on CALayers
. However I am unable to set multiple corners to be rounded at the same time in Objective-C.
The swift implementation is:
view.layer.cornerRadius = 10
view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
But in Objective-C this does not work:
view.layer.maskedCorners = @[kCALayerMinXMinYCorner, kCALayerMaxXMinYCorner];
I think this is because Swift is using an OptionSetType
but I don't know what the Objective-C equivalent to that is.
Has anyone been able to successfully use multiple masked corners with Objective-C? Thanks in advance for your help.
Typically, your C++ code will define some class you'd like to use. You could switch your whole project to Objective-C++ by renaming all the .m files to .mm, and freely mix C++ and Objective-C.
Name your project and select the language as Objective C. Select the desired location and save your project. Now, in the Project Navigator, you will see a 'main' file. This file is similar to the main function in C programming. The code execution starts from here. To add new files, right-click on your Project and select New File.
The compiler will get enormously confused by class CppObject and the block following it, as that's simply not valid Objective-C syntax. The error will typically be something like.
To fix this issue, rename ViewController.m to ViewController.mm. This simple naming convention tells Xcode that ViewController wants to mix Objective-C with C++. After renaming the file, the error should disappear. Let’s make the app more interactive by adding a button.
In Objective-C you need to use bitwise OR (|
) to combine the values:
view.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With