【发布时间】:2011-02-18 17:20:54
【问题描述】:
如何在 iPhone 上制作一个放置在 UItoolbar 左侧的按钮?
【问题讨论】:
-
请接受对您之前问题的一些回答,否则人们将没有动力帮助您。如果您对答案感到满意,请单击答案左侧的复选标记。
标签: iphone
如何在 iPhone 上制作一个放置在 UItoolbar 左侧的按钮?
【问题讨论】:
标签: iphone
例如,你需要这样的东西。
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]];
【讨论】:
您需要两个 UIBarButtonItem。一个是您的按钮,一个是灵活的空间,在放置所有项目后将尽可能多地占用酒吧的空间。根据您设置项目的顺序,按钮将出现在最左侧或最右侧。如果您先添加空格,然后添加按钮,则所有按钮都会右对齐。反之,您的按钮将位于最左侧。
试试这个:
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];
【讨论】: