【问题标题】:how to set label to center in uitoolbar如何在uitoolbar中将标签设置为居中
【发布时间】:2012-11-24 12:16:44
【问题描述】:

我正在为 iOS 开发一个通用应用程序,因此我必须将标签设置为UIToolBar 的中心。请帮我做这件事。

我已经实现的代码

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
label.backgroundColor = [UIColor clearColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd-MM-yyyy  hh:mm a"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
label.text = dateString;

【问题讨论】:

标签: iphone objective-c uitoolbar


【解决方案1】:

FirstViewController.h

@interface FirstViewController:UIVieWController
{
   UILable *justLabel;
}
@end  

在 FirstViewController.m 中尝试下面的代码。这里我正在使用计时器来在 UIToolbar 上显示时间

 @implementation FirstViewController

 -(void)viewDidLoad
    {
        UIToolbar *tool=[[UIToolbar alloc] initWithFrame:CGRectMake(0,372,   self.view.frame.size.width, 44)];  

        justLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 4, 250, 40)];
            justLabel.backgroundColor = [UIColor clearColor];
        [self.view addSubview:tool];
        [tool addSubview:justLabel];            

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:YES];  


     }

-(void)timer
{

       NSDateFormatter *format = [[NSDateFormatter alloc] init];
       [format setDateFormat:@"dd-MM-yyyy  hh:mm:ss a"];
       NSDate *now = [NSDate date];
       NSString *dateString = [format stringFromDate:now];
       justLabel.text=dateString;

}

@end

【讨论】:

    【解决方案2】:

    您必须在标签前后添加灵活的空格键按钮项目,这将使标签在工具栏中居中。

    弹性空间是创建 UIBarButtonItems 时的系统项之一。当您设置工具栏的项目时,它应该是一个包含一个灵活空间、您的标签和另一个灵活空间的数组。

    【讨论】:

      【解决方案3】:

      根据视图宽度设置标签的边框......

      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-250)/2, 0, 250, 40)];
      

      (self.view.frame.size.width-250)/2 中的 250 是您所需的标签宽度.....

      【讨论】:

      • 我更改了以下更改,但它对我有用,它需要 x,y vales。
      • 通过设置页面宽度我可以实现这个任务 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width)-55, 20)];
      【解决方案4】:

      通过设置页面我是

      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width)-55, 20)];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2016-10-14
        • 2011-01-12
        • 1970-01-01
        • 2012-01-08
        • 1970-01-01
        相关资源
        最近更新 更多