简体   繁体   English

UIButton背景不包括整个视图

[英]UIButton background doesn't cover whole view

I use uibutton to display some text in title ,sometimes text is long and I set 我使用uibutton在标题中显示一些文本,有时文本很长,我设置

text.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; 

when there are 2 or more lines , background doesn't have enough height 当有2行或更多行时,背景没有足够的高度 在此输入图像描述

This is my code for button: 这是我的按钮代码:

text = new UIButton ();
        text.Layer.CornerRadius = 5;
        text.Font = UIFont.FromName ("DIN Condensed", text.Font.PointSize);

        text.SetTitle (Original._hint, UIControlState.Normal);
        text.SizeToFit ();
        text.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; 

        var rec = new UILongPressGestureRecognizer (() => {     

            text.Frame = new CGRect ((UIApplication.SharedApplication.KeyWindow.Frame.Width / 2.0f) - (250 / 2.0f),
                this.Superview.Superview.Superview.Frame.Height / 2.0f,250, text.Frame.Height);
            Animate (0.6f, () => {
                this.Superview.Superview.Superview.AddSubview (text);
            }, () => {
                text.Layer.ZPosition = int.MaxValue;
            });

            viewToremove=text;
        });


        text.SetTitleColor (UIColor.White, UIControlState.Normal);
        text.BackgroundColor = UIColor.Black;

        TargetView.AddGestureRecognizer (rec);

To makes things more clear: 为了使事情更清楚:

  1. Make your image strechable, that can be easily done in the image asset directory by showing slicing here , this will guarantee that you button will keep the same aspect also in the corners 通过在此处显示切片,可以在图像资源目录中轻松完成图像的可伸缩性,这将保证您的按钮在角落中也保持相同的方面
  2. Use button inset, you can do it in IB 使用按钮插图,你可以在IB中完成 插页 Than I would avoid to use add a gesture recognizer to a UIButton, but I think it will work anyway. 我会避免使用为UIButton添加手势识别器,但我认为无论如何它都会起作用。

I think your problem might be cause by your .SizeToFit() method. 我认为您的问题可能是由.SizeToFit()方法引起的。 Generally, you want the .SizeToFit() method to be the last modification of a UIButton, or any other control for that matter. 通常,您希望.SizeToFit()方法是UIButton的最后一次修改,或任何其他控件。 This is to prevent .Frame issues, and text not properly displaying. 这是为了防止.Frame问题,并且文本没有正确显示。 This probably goes for the background of the UIButton too. 这可能也适用于UIButton的背景。 So: 所以:

  • Initialise your button (I usually keep the width static (screenwidth) and set the height to a high number, like 200 or something). 初始化按钮(我通常保持宽度静态(屏幕宽度)并将高度设置为高数字,如200或其他)。
  • Modify its properties (including .Title and .BackgroundColor) 修改其属性(包括.Title和.BackgroundColor)
  • Add a delegate or eventhandler. 添加委托或事件处理程序。
  • Shrink the button using .SizeToFit(); 使用.SizeToFit()缩小按钮;

Let me know if it works. 如果有效,请告诉我。 Good luck! 祝好运!

Love and regards, Björn 爱和问候,Björn

Have you tried using a 9patch drawable as your background image? 您是否尝试过使用9patch drawable作为背景图片? Those are designed specifically for situations where you are unsure about the length of the content and require your background image to stretch/shrink to fit. 这些设计专门针对您不确定内容长度并要求背景图像拉伸/缩小以适应的情况。 More info here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM