定制UIBarButtonSystemItem与其他文本

问题描述:

我想与其他文本的UIBarButonSystemItem(类似于例如UIBarButtonSystemItem.Cancel)我的底部工具栏上。定制UIBarButtonSystemItem与其他文本

这是我现在有(代码是在C#中,但它并不重要,因为你可以在Objective-C提供解决方案):

UILabel markLabel = new UILabel (new RectangleF(0,0,150,30)); 
markLabel.Text = "Mark"; 
UIView containerView = new UIView (new RectangleF(0,0,150,30)); 
containerView.AddSubview (markLabel); 
var markButton = new UIBarButtonItem (markLabel); 

应该如何这样的按钮产生的呢?使用UIButton(我这里失败)或UIView与上面UILabel?此外,我始终以提供框架性能。如果您想使用自动布局,该怎么办?尺寸如何适应不断变化的内容尺寸(例如国际化)以及间距元素呢?

编辑:

这现在与UIButton

var temp = UIButton.FromType (UIButtonType.System); 
temp.SetTitle ("Mark", UIControlState.Normal); 
temp.SizeToFit(); 
var markButton = new UIBarButtonItem (temp); 

但颜色和字体大小必须适应。有些问题仍然存在:如果文本变得更宽,它不会缩小字体大小甚至AdjustsFontSizeToFitWidth使用。定位也不同。例如。取消按钮与屏幕边框的间距不同于我的按钮。

一个按钮可以使用UIButton这样进行:

UIButton button = new UIButton (new RectangleF(5, 5, 30, 30)); 
button.SetTitle ("Hello, world", UIControlState.Normal); 

在此实例中,框架属性是硬编码的。如果你想让大小自动调整,你应该添加属性到一个可以从任何其他类中调用的类(比如AppDelegate)。

public static float ScreenWidth { get; set; } 
public static float ScreenHeight { get; set; } 

然后,添加一个调用的第一个视图的ViewWillAppear新属性将出现在您的屏幕(更新帧尺寸后),并在您想要控制所有类的ViewDidRotate方法是自动调节的(事后还更新帧大小):

public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation) 
{ 
    base.DidRotate (fromInterfaceOrientation); 

    AppDelegate.ScreenWidth = this.View.Bounds.Width; 
    AppDelegate.ScreenHeight = this.View.Bounds.Height; 

    UpdateFrameSize(); 
} 

字体大小可以与button.Font属性被改变:

button.Font = UIFont.SystemFontOfSize (12); 

可悲的是,没有选项按钮文本换行。如果要将文本显示并包装以适合屏幕,则应在接下来的按钮上方或下方使用UILabel,该按钮将显示要显示的文本。您可以使用的UILabel的UILabel.LinesUILabel.LineBreakMode性能。

lblClient.Lines = 5; 
    lblClient.LineBreakMode = UILineBreakMode.WordWrap; 

用这些属性和方法试验一下,看看什么最适合你的项目。如果事情不清楚,或者希望对示例中的问题进行更多解释,请不要犹豫。祝你好运!

+0

感谢Magicbjørn为您解答。我得到它设法使用我在我的编辑发布的解决方案。 'AdjustsFontSizeToFitWidth'和'SizeToFit'不能一起工作。你只能使用一个。随着间距,我采取了负面的间隔。但以这种方式,我不必跟踪屏幕尺寸。但有第二备份选项很不错。 – testing 2015-01-07 08:06:14