Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extended UIButton border is not initially drawn

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.

Before-After Screenshot

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;

Red Background Test, Before-After Screenshot

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) { } } 
  • I'm just not sure how to force the border to be drawn correctly on loading of the view.
  • I don't need a button of type UIButtonType.Custom, as I don't want to style the button myself.
  • When I debug I the type of MyButton is correctly set to UIButtonType.RoundedRect.
  • The UIButton base properties of MyButton (button2) match the properties of the UIButton instance (button1).

Debug

How can I resolve this issue?


Update (January 31st 2013): Herman Schoenfeld provided a suitable solution for this bug.

like image 676
Scott Avatar asked Dec 29 '12 11:12

Scott


People also ask

Can I add a background color to UIButton States in Interface Builder?

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.

What are the different states of a UIButton?

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.

How do I change the background image of a UIButton?

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.


1 Answers

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.

like image 62
Herman Schoenfeld Avatar answered Oct 04 '22 06:10

Herman Schoenfeld