I am trying to create a custom UIButton which extends from UIButtonType.RoundedRect
.
My added functionality is working, but there is an issue with the initial rounded border state of my button. The border of my extended button is not drawn until after it has been tapped.
Update (January 24th 2013): Added the result of red background test, as requested by Richard Marskell, which concludes only the label of the button is drawn. BackgroundColor = UIColor.Red;
Below is my source code for creating my custom button.
public class TestView : UIView { public TestView(IntPtr p) : base(p) { } public TestView(RectangleF bounds) { Frame = bounds; BackgroundColor = UIColor.White; UIButton button1 = new UIButton(UIButtonType.RoundedRect); button1.Frame = new RectangleF(20,20,100,50); button1.SetTitle("Button 1", UIControlState.Normal); AddSubview(button1); // Drawn Correctly MyButton button2 = new MyButton(); button2.Frame = new RectangleF(20,90,100,50); button2.SetTitle("Button 2", UIControlState.Normal); AddSubview(button2); // Only drawn correctly after Tap // EDIT: Added to test Miguel's theory UIButton button3 = UIButton.FromType(UIButtonType.RoundedRect); button3.Frame = new RectangleF(20,160,100,50); button3.SetTitle("Button 3", UIControlState.Normal); AddSubview(button3); // Drawn Correctly } } public class MyButton : UIButton { public MyButton() : base(UIButtonType.RoundedRect) { } }
UIButtonType.Custom
, as I don't want to style the button myself.UIButtonType.RoundedRect
.How can I resolve this issue?
Update (January 31st 2013): Herman Schoenfeld provided a suitable solution for this bug.
We already have a background color for UIButton that can be used for the default state, so that means we need to add a color to Interface Builder for the disabled, selected, and highlighted state. Unfortunately, we can’t add our color to the grouping of properties that change for each of the button states.
A UIButton can be in many states: default, selected, highlighted, or disabled. There are also many items on a UIButton that you can customize for the different states. For instance, you can change the title, font, text color, shadow color, image, and background image, but not the background color. For some reason, Apple decided not to include it.
The first option involves using a full-sized graphic as the background of your UIButton instance. To do so, design the button (normal and highlighted states) in your favorite graphic editor then export the assets as uncompressed PNGs in both standard and double resolutions.
This works
public class MyButton : UIButton { public MyButton() : base(UIButtonType.RoundedRect) { } public override RectangleF Frame { get { return base.Frame; } set { var temp = TranslatesAutoresizingMaskIntoConstraints; TranslatesAutoresizingMaskIntoConstraints = false; var constraints = new [] { NSLayoutConstraint.Create(this, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, value.Width), NSLayoutConstraint.Create(this, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, value.Height) }; AddConstraints(constraints); SizeToFit(); RemoveConstraints(constraints); base.Frame = value; TranslatesAutoresizingMaskIntoConstraints = temp; } } }
This is only a workaround, it appears to be a bug. The SizeToFit() fixes the issue, the other code maintains the frame.
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