gpt4 book ai didi

ios - 如何将导航栏按钮放置在自定义位置

转载 作者:行者123 更新时间:2023-11-29 03:52:36 25 4
gpt4 key购买 nike

我当前的导航栏如下所示:

enter image description here

如您所见,我需要将其移至右侧。不确定我的设置是否正确,有什么建议吗?

代码如下:

-(void)customizeNavigationBar{

//*** Nav bar Background ***
//Image being used
UIImage *backgroundImage = [UIImage imageNamed:@"navBarBackground"];

//Set navigation bar image
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

//*** Search button**
//Get Image
UIImage* searchImage = [UIImage imageNamed:@"navSearchButton"];

//Set frame
CGRect frame = CGRectMake(0, 0, searchImage.size.width, searchImage.size.height);

//Create button
UIButton *searchButton = [[UIButton alloc] initWithFrame:frame];

//Set background images (selected and not)
[searchButton setBackgroundImage:searchImage forState:UIControlStateNormal];
[searchButton setBackgroundImage:[UIImage imageNamed:@"navSearchButtonSelected"] forState:UIControlStateSelected];

//Add action
[searchButton addTarget:self action:@selector(search)
forControlEvents:UIControlEventTouchUpInside];

//Set highlight on
[searchButton setShowsTouchWhenHighlighted:YES];

//Create nav buttons
UIBarButtonItem *navSearchBtn =[[UIBarButtonItem alloc] initWithCustomView:searchButton];

//Add to nav bar
[self.navigationItem setLeftBarButtonItem:navSearchBtn];
}

最佳答案

  • 使用 setImage:forState: 将背景图像设置为 UIButton(而不是 setBackgroundImage:)
  • 将 UIButton 的框架设置得大一点(size.width)

关于ios - 如何将导航栏按钮放置在自定义位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16924821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com