【问题标题】:want to add to buttons on top of every view in iPhone想要添加到 iPhone 中每个视图顶部的按钮
【发布时间】:2010-11-30 12:48:00
【问题描述】:

我想在我的 iPhone 应用程序的每个视图顶部添加两个宽度分别为 133 像素和 150 像素、高度为 28 像素的按钮。只有这个按钮栏下面的视图会改变,但是当导航发生时(推弹操作),我希望这个栏也与剩余的视图一起滑动。 基本上,我希望在应用程序的任何屏幕中都出现一个包含 2 个按钮的栏。请帮我。谢谢

【问题讨论】:

  • 您是在使用顶部导航栏的UINavigationController 并希望将您的按钮放在导航栏下方,还是希望将您的按钮放在导航栏中?
  • 不,我正在使用导航栏,但它隐藏在所有屏幕中,所以这个栏将在那里代替
  • 你为什么要这样做?您确定可以在导航栏中获得所需的所有功能吗?
  • 我的按钮很长,所以无法添加到导航栏中,这就是原因。如果您对如何在左右角添加两个长按钮(无titleView)有一些建议,请帮助。谢谢
  • 将 titleView 设置为 nil 以删除标题,然后添加 leftBarButtonItem 和 rightBarButtonItem。如果您希望我对此进行扩展,我可以。

标签: iphone uiview uinavigationcontroller


【解决方案1】:

你最好写一个 UIViewController 的子类(例如叫它CustomUIViewController)。

在该类的 viewDidLoad 方法中,您可以根据需要添加视图,例如:

- (void)viewDidLoad {
    UIView *bar = [[[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,28)] autorelease];
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // etc...
    [bar addSubview:buttonOne];
    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // etc...
    [bar addSubview:buttonTwo];

    [self.view insertSubview:bar atIndex:0];
}

这应该可以(只要您记得在所有课程中都调用[super viewDidLoad]

现在您只需确保在视图控制器中继承 CustomUIViewController 而不是 UIViewController。

编辑

要使导航栏选项起作用,请执行以下操作:

    UIBarButtonItem *cancel = [[[UIBarButtonItem alloc] initWithTitle:@"Cancelsdkjfnsdjfhksdjfhks" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)] autorelease];
    [self.navigationItem setLeftBarButtonItem:cancel];
    UIBarButtonItem *send = [[[UIBarButtonItem alloc] initWithTitle:@"Senddsfsdfsdfsdfsdfsdf" style:UIBarButtonItemStylePlain target:self action:@selector(send:)] autorelease];
    [self.navigationItem setRightBarButtonItem:send];

    self.navigationItem.titleView = [[[UIView alloc] init] autorelease];

【讨论】:

  • 感谢您的回答,这肯定是一种可能性,但您能否提供一个解决方案,让我了解如何在 UINavigationBar 上添加长按钮作为 UIBarButtonItems
  • 我在导航栏中通过 initWithCustomView: uibutton 尝试了这种方法,因为我有按钮的图像而不是标题。这是行不通的。导航栏为空白
  • 我搞定了,你必须初始化一个带有目标、动作集的按钮,并将原点的按钮框架设置为 0,0。然后使用 initWithCustomView:button 初始化 barButton。感谢您指明方向。
【解决方案2】:

我们将从布置一个包含按钮的视图开始。在 Xcode 中,创建一个新的基于 View 的 iPhone 应用程序,并将其命名为“GradientButtons”(或任何您想要的名称)。双击 GradientButtonsViewController.xib 资源,在 Interface Builder 中打开它。

将 UIButton 控件从库中拖到视图上。像这样设置它的属性:

* Change the type to "Custom"
* Set the title to "Gray"
* Change the text color to white
* Change the background color to gray

【讨论】:

  • 你没有得到我的问题。我想添加一个包含两个按钮的栏作为其子视图。现在这个栏应该出现在应用程序的每个视图/屏幕中。请看问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多