在iOS中,可以使用UINavigationBar类来创建导航栏。下面是一个简单的示例,演示了如何使用纯代码创建具有两个按钮的导航栏:

// 创建导航栏
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
navigationBar.backgroundColor = [UIColor whiteColor];

// 创建导航栏左侧按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
navigationBar.topItem.leftBarButtonItem = leftButton;

// 创建导航栏右侧按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneAction)];
navigationBar.topItem.rightBarButtonItem = rightButton;

// 将导航栏添加到视图中
[self.view addSubview:navigationBar];

在上面的示例中,我们首先创建了一个UINavigationBar对象并设置了其背景颜色。然后,我们创建了左侧和右侧的UIBarButtonItem对象,并将它们分别添加到导航栏的左侧和右侧按钮中。最后,我们将导航栏添加到视图中。

需要注意的是,在创建UIBarButtonItem对象时,需要指定其样式、标题、目标和动作。样式可以是Plain、Bordered、Done等,而目标和动作则是在按钮被点击时执行的方法。

如果需要在导航栏中添加更多的按钮,可以使用UINavigationItem对象来管理导航栏上的内容。例如,可以使用UINavigationItem的title属性设置导航栏的标题,使用leftBarButtonItems和rightBarButtonItems属性设置左侧和右侧的按钮数组。

用iOS写个导航栏用纯代码写

原文地址: http://www.cveoy.top/t/topic/bFie 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录