以编程方式在底部的UIToolbar

Mc.*_*ver 7 iphone objective-c uitoolbar ipad ios

我正在以UIToolbar编程方式创建,但问题是此工具栏的位置是否向上(导航栏位置).如何将其自动放在底部?

这是我的代码:

    CGRect rect2 = CGRectMake(0, toolBar.frame.origin.y , self.view.frame.size.width , 0);
    toolBar = [[UIToolbar alloc]initWithFrame:rect2];
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    [toolBar sizeToFit];
    [self.view addSubview:toolBar];
    [toolBar release];
Run Code Online (Sandbox Code Playgroud)

因为我的应用程序是通用的,我的视图控制器类没有任何nib文件我需要为iPad和iPhone定义它,我不想使用UIUserInterfaceIdiomPad.

Jas*_*ien 11

您正在设定rect2y位置值[toolbar frame].origin.y,其在代码中的那点要么nil,或指向工具栏的其他一些情况下,因为你然后随即allocinit新的工具栏.

即使工具栏在设置框架时有效,也不能将其当前y值用作新y值,因为它将是0.

您应该相对于屏幕底部定位它,减去工具栏的高度.试试这个:

CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);
Run Code Online (Sandbox Code Playgroud)


neo*_*eye 9

这将创建一个底部对齐的工具栏

CGRect frame, remain;
CGRectDivide(self.view.bounds, &frame, &remain, 44, CGRectMaxYEdge);
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];
[toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
[self.view addSubview:toolbar];
Run Code Online (Sandbox Code Playgroud)