Custom Drawn UIButton - ios

I need to have a circular UIButton that draws a custom color in the middle, along with an outer ring that is drawn as the default tint color (similar to a UIColorWell).
I've tried a few different approaches:
Using a multicolor SFSymbol: This would be an elegant solution, but as far as I can tell there's no way to apply the tint color to just a part of the image while setting the center to be a custom color. Either the entire image is tinted, or the image is drawn as the default colors set in the symbol file. Also, I need to support iOS 14, while the new hierarchical options that may allow me to accomplish this were added to iOS 15.
Setting various layer properties (ie, cornerRadius, borderColor, etc): This works and may be a decent fallback solution, but I'm unable to get the look that I'm going for (namely, having a transparent ring between the outer border and inner colored circle).
If there's a way to use either of the above options, please educate me! Either one seems like a better solution than:
Overloading the draw function: This is the option I'm going with at the moment, as it allows me to have complete creative control over the look of the button. The rest of this post will be regarding this method.
So far I was able to get the button to be drawn exactly as I wanted. However, I am unable to figure out how to draw the button appropriately with regards to various state changes.
For example, if a popover is displayed, all of the normal buttons are automatically redrawn as disabled. My custom button, however, isn't redrawn so I am unable to respond to the state change.
Same thing with tapping on the button - normal buttons are briefly shown in an emphasized color but my custom button doesn't respond.
Does someone have an example as to how to support overriding UIButton drawing with various states?

I was able to get the desired behavior by overriding tintColorDidChange in order to trigger a redraw of the button. Now I am able to draw the outer ring in the correct color (including grayed out when a popover is displayed) while maintaining the desired inner color.

Related

Change text color based on background color (e.g. in a progress bar)

I find it difficult to describe what I want to achieve. I looked for a picture to show:
(image link: https://d13yacurqjgara.cloudfront.net/users/7017/screenshots/351285/button.gif)
You see that the text color of "3/4" changes to white when the bar reaches it. How can I achieve this with swift?
My idea is to have two UIViews, with the exact same content (except for the text color) and blend one over the other by resizing a mask or something.
The problem is that I would have to deal with duplicate content. If I wanted to change something, I would have to change it inside both views. Is there a more convenient way?
Thank you!

iOS - blurred view with moving colors (like siri on iOS 9)

I'm writing an iOS application, and I would like to use a view that has randomized moving colors in the background, much like the view that is brought up when you use siri. The view used for siri seems to be slightly darker than a standard dark UIBlurEffect and there are random colors added that move slowly around the view whenever queries are made.
Below is an example of the background I'd like to create. There's some red in the top right of the blur and some green in the top left. Plus some light purple or something at the bottom.
siri query view
The colors fade and new colors are added every queries, I honestly have no idea how to accomplish this, so any help that anyone has would be great.
Also, I'd like the answer to be in Objective-C if at all possible.
You refer to UIBlurEffect and it is key here. It provides a blur over a background, but doesn't specify what that background is. So, create that background to be a set of coloured views which appear (fade in perhaps), move around and then disappear. How you control the start and end location and the path the views follow is up to you. The effects view will apply the blurring for you and you can add whatever content you want in the foreground to the content of the effects view.

How to specify vibrant appearance for secondary Today Centre widget text?

I am working on my first Today Centre widget, which simply consists of three labels on the default storyboard. It's working fine, but I read in the iOS Human Interface Guidelines that secondary text should "use the system-provided vibrant appearance" with notificationCenterVibrancyEffect.
What's the correct way to add this vibrancy effect to my two secondary labels? I've read about UIVisualEffectView, but it's still not clear to me how to use it for this purpose. I don't think I want to put a blurred view behind my labels because Notification Centre already blurs the background.
UIVisualEffectView when configured with a UIVibrancyEffect is meant to be used as a container—just drop your labels in its contentView and you’ll get the appropriate appearance on top of the Notification Center blur. The text color doesn’t matter; when it’s added to the contentView it gets special treatment and effectively always renders with the same appearance, though you can still adjust the alpha of the view (not of the text color) to make it more or less prominent.
good day, first of all u should use a correct view hierarchy, when U add some objects in your VisualEffectView example:
UIVibrancyEffect used in combination with UIVisualEffectView, adjusts the colors of the content to make it feel more transparent. UIVibrancyEffect can blending some objects
with the background example:
More particularly to your question(https://stackoverflow.com/a/25392645/4912496) Apple recommends to use vibrant effect for secondary text, because its simply beautiful for example(apple native app use vibrant effect, and Sports.ru don't use it):
But, some apps don't use this effect...
To ensure that your widget gets the vibrancy effect that’s appropriate for displaying items in the Today view, use notificationCenterVibrancyEffect.
Some info. https://www.omnigroup.com/developer/how-to-make-text-in-a-uivisualeffectview-readable-on-any-background

How to create buttons like in notification window on iOS8?

How can I create a button in a Today widget like the "edit button" in the picture below?
Is there a quick and standard way to do this or must I subclass a button and override it's draw method to draw a filled and rounded rectangle with transparency and background-only highlighting when touched down?
I am trying to find the best way to create exactly the same visuals for a button, instead of trying to imitate and match behaviour of it. Is it possible?
Segmented controls are a default within Xcode. It does most of the heavy lifting for you.
The UIKit Interfact Catalog in the iOS developer library should be helpful for you.
Within Xcode:
You will find segmented controls within the Interface Builder, in the Segmented Control section of the Attributes Inspector.
From here you can begin to build the visual styling and tie it to your code.
This should point you in the right direction.
More from apple is here: Segmented Control in the iOS developer library
They have great documentation, be sure you always seek that out.
From some quick tinkering I've got a UIButton that looks relatively close, you can tinker it some more to get it perfect. Here's a picture:
My buttons settings (the top one) has a height of 28, corner radius of 3, font color of black, and a background color of white with 31% Opacity. In the picture I was using text size of 15 but 13 or 14 looks a lot more accurate. I used the answers on this question as a reference point on how to create the rounded buttons.
Hope this helps some!

UIButton with background image highlighted state wrong

I'm trying to use the standard highlighting logic for a UIButton but set the background image for the Normal state. When do this programmatically everything works fine, but when my button is set in Interface Builder I get the following results:
Has anyone run in this where the standard highlighting is changing the way the background image looks? Just to be clear I want the color change, just not the squared off corners on the left.
Figured it out. Apparently if my button size is smaller than the background image it scales appropriately for Normal State, but the highlighted state causes this strangeness. Good to know since I'm attempting to use a single image and just scale it for all of my various buttons.
In this case my IB button was set as 77pt wide, but my image is 97pt wide. It finally looks correct with my IB button set at 90pt wide or larger.

Resources