如何在iPhone的工具栏左侧制作按钮

时间:2011-02-18 07:44:39

标签: iphone

如何制作位于iPhone上UI工具栏左侧的按钮?

2 个答案:

答案 0 :(得分:4)

举个例子,你需要这样的东西。

    UIBarButtonItem *fixedCenter = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedCenter.width = 80.0f;
    UIBarButtonItem *fixedLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedLeft.width = 40.0f;

    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left.png"] style:UIBarButtonItemStylePlain target:self action:@selector(moveBack:)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"right.png"] style:UIBarButtonItemStylePlain target:self action:@selector(moveForward:)];

    [self setToolbarItems:[NSArray arrayWithObjects:fixedLeft, flex, left, fixedCenter, right, flex, action, nil]];

答案 1 :(得分:0)

您需要两个UIBarButtonItems。一个是你的按钮,一个是灵活的空间,在放置所有物品后,它将占据酒吧的尽可能多的空间。根据您设置项目的顺序,该按钮将显示在最左侧或右侧。如果先添加空格然后再添加按钮,则所有按钮都对齐。反过来,你的按钮将放在最左边。

试试这个:

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Button Title" style:UIBarButtonItemStylePlain target:self action:@selector(yourMethod:)];


[toolbar setItems:[NSArray arrayWithObjects:leftButton, flexibleSpace, nil]];
[flexSpace release];
[leftButton release];