想要添加到iPhone中每个视图顶部的按钮

时间:2010-11-30 12:48:01

标签: iphone uiview uinavigationcontroller

我想在iPhone应用程序的每个视图顶部添加两个宽度分别为133像素和150像素,高度为28像素的按钮。只有此按钮栏下方的视图才会更改,但是当导航发生时(按下弹出操作),我希望此栏也可以与剩余视图一起滑动。 基本上,我希望在应用程序的屏幕上显示包含2个按钮的栏。请帮我。感谢

2 个答案:

答案 0 :(得分:2)

你最好写一个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];

答案 1 :(得分:-1)

我们首先要布置一个包含按钮的视图。在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