resetting the background image of a UITextField doesn't work - ios

Setting a custom background image (for iOS < 5) in the style of a tinted rounded rect text field works fine,
yet I can't change it back to normal or not even to another background image, once it's set.
I'm calling setBackground:nil and setBorderStyle:UITextBorderStyleRoundedRect on the view in order to reset it.
There is some effect: The edge of the border gets slightly lighter, as if the desired view is drawn behind it, but it's not set back to the normal appearance. Calling setNeedsDisplay doesn't help either.
Am I missing another property?

Related

Custom Drawn UIButton

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.

How to change UIPopoverPresentationController's arrow color without drawing my own

I want to change the color of the arrow that UIPopoverPresentationController uses to point at its source. The only way I can see to do this is to subclass UIPopoverBackgroundView and write drawRect and draw the popover border and arrow myself. That's a pain, because there are little niceties in the default popover like rounded corners on everything. And I want to make it feel as much at home on the platform as I can.
Do I really have to draw my own popover arrow just to change the color of the arrow?
Update: I've tried setting the backgroundColor of the UIPopoverPresentationController, but it appears to change the color of everywhere the arrow might draw. I've seen it work before, but I can't nail down what makes it work when it does. See screenshot of setting the color to red. The blue is the content of the navigation controller in the popover. It's supposed to be pointing at the button.
The UIPopoverPresentationController's backgroundColor property should handle that for you
Update
I've used this approach a number of times and have never seen what you have in your screenshot. Are you sure you are using popoverPresentationController.backgroundColor and not setting a different background color on a view or container? Below is screenshot of non-centered popover arrow. The view controller background is green, the popoverPresentationController.backgroundColor is red. Shown next to the code setting the value.
Update #2
I looked at the project you posted and found the problem (although I'm not entirely sure why it's the problem). You are setting the popover presentation controller's backgroundColor property inside your presented view controller under viewWillAppear:. I suspect that setting the background color like this after the presentation happened is what triggers the bug pre-iOS 10.
If you set the popover presentation controller's backgroundColor inside your presenting view controller's onPopover: method, where you are also setting the sourceView and sourceRect properties (and before you actually call presentViewController:), everything works correctly.

UIWebView Rotation and Background Color

I've run into an interesting situation with a UIWebView. In Interface Builder, I set all four constraints to equal the margins and filled the webview with content. It works exactly as you'd expect.
An issue occurs when I rotate the webview. The webview rotates but in doing so, it reveals an grey background color. This isn't a color that I've set. In fact, I set the parent's view to have a black background color to match the webview content.
Here are my constraints for reference:
I'd appreciate any guidance on how to make my webview will the entire border or at least, manually change the grey color. Much thanks!
Note: This only occurs in the simulator. I haven't tried this on a device yet.
Ack ... it was the background view that was the culprit. The simulator didn't pick up my color changes. I never did a clean on them. I tested today and was able to change the grey color to the one of my choosing.

XCode UIButton confusion

I'm having some confusion about UIButton on the storyboard. I create a new button, change the background color, and the color is always darker than what I choose. In this example, the actual color that is being displayed is #163ca1 (22, 60, 161), even though I clearly picked something lighter:
Wassup with that? The thing is, if I set the background color programmatically it displays the correct color. This issue extends to other view elements as well.
Also, what does Tint color do? It seems like it has a different function depending on the element in question, but in this case it seems to have no effect. I figured it was for the highlighted state, but no matter what I change it to the button text maintains a light blue color on click.

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