RightBarButtonItem 仅显示为非活动状态
RightBarButtonItem shows only as inactive
我在尝试将刷新按钮显示为 rightBarButtonItem 时遇到一个奇怪的问题。
总之,我已经实现了,但是运行应用程序什么都看不到。然而,当我点击故事板时 Debug --> View Debugging --> Capture View Hierarchy
。我可以看到一个似乎不活动且隐藏的刷新按钮。我不知道为什么。
viewcontrol 实际上是通过自定义 pageviewcontroller 推入的。
- (void)viewDidLoad {
[super viewDidLoad];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
TNViewController *initialViewController = [self viewControllerAtIndex:currentIndex];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(refreshClicked:)];
initialViewController.navigationItem.rightBarButtonItem = refreshButton;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:initialViewController];
NSArray *viewControllers = [NSArray arrayWithObject:navigationController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
for (UIView *subview in self.pageController.view.subviews) {
if ([subview isKindOfClass:[UIPageControl class]]) {
UIPageControl *pageControl = (UIPageControl *)subview;
pageControl.pageIndicatorTintColor = [UIColor blackColor];
pageControl.currentPageIndicatorTintColor = [utils colorFromHexString:@"#AA3635"];
pageControl.numberOfPages = _news.count;
pageControl.backgroundColor = [UIColor whiteColor];
}
}
self.edgesForExtendedLayout = UIRectEdgeTop;
}
请问我错过了什么?
因为您要将它添加到 uiviewcontroller,所以您需要创建 UINavigationBar、UINavigationItem 和 UIButton。然后将 UIButton 添加到 UINavigationItem,然后将 UINavigationItem 添加到 UINavigationBar:
_navBar = [[UINavigationBar alloc] init];
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
[self.view addSubview:_navBar];
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClicked:)];
[navItem setLeftBarButtonItem:barButton];
[_navBar setItems:@[navItem]];
此代码来自此处找到的答案:
adding barButtonItems to a UINavigationBar without a Navigation Controller
希望对您有所帮助。
您是否尝试在实例化 NavigationController 后添加它?
终于找到问题了。在这种情况下非常令人困惑的是,实际的 TNViewcontroller
被包裹在 TNPViewController
中。后者是 PageViewController。我一直试图在 TNViewController
而不是 TNPViewController
上添加 navigationItem。
这是正确的方法,而且有效。
ParentViewController:
TNPViewController *tnp = [[TNPViewController alloc] initWithNews:news index:indexPath.row];
[[self navigationController] pushViewController:tnp animated:YES];
TNPViewController:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(refreshClicked:)];
self.navigationItem.rightBarButtonItem = refreshButton;
}
我在尝试将刷新按钮显示为 rightBarButtonItem 时遇到一个奇怪的问题。
总之,我已经实现了,但是运行应用程序什么都看不到。然而,当我点击故事板时 Debug --> View Debugging --> Capture View Hierarchy
。我可以看到一个似乎不活动且隐藏的刷新按钮。我不知道为什么。
viewcontrol 实际上是通过自定义 pageviewcontroller 推入的。
- (void)viewDidLoad {
[super viewDidLoad];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
TNViewController *initialViewController = [self viewControllerAtIndex:currentIndex];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(refreshClicked:)];
initialViewController.navigationItem.rightBarButtonItem = refreshButton;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:initialViewController];
NSArray *viewControllers = [NSArray arrayWithObject:navigationController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
for (UIView *subview in self.pageController.view.subviews) {
if ([subview isKindOfClass:[UIPageControl class]]) {
UIPageControl *pageControl = (UIPageControl *)subview;
pageControl.pageIndicatorTintColor = [UIColor blackColor];
pageControl.currentPageIndicatorTintColor = [utils colorFromHexString:@"#AA3635"];
pageControl.numberOfPages = _news.count;
pageControl.backgroundColor = [UIColor whiteColor];
}
}
self.edgesForExtendedLayout = UIRectEdgeTop;
}
请问我错过了什么?
因为您要将它添加到 uiviewcontroller,所以您需要创建 UINavigationBar、UINavigationItem 和 UIButton。然后将 UIButton 添加到 UINavigationItem,然后将 UINavigationItem 添加到 UINavigationBar:
_navBar = [[UINavigationBar alloc] init];
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
[self.view addSubview:_navBar];
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClicked:)];
[navItem setLeftBarButtonItem:barButton];
[_navBar setItems:@[navItem]];
此代码来自此处找到的答案:
adding barButtonItems to a UINavigationBar without a Navigation Controller
希望对您有所帮助。
您是否尝试在实例化 NavigationController 后添加它?
终于找到问题了。在这种情况下非常令人困惑的是,实际的 TNViewcontroller
被包裹在 TNPViewController
中。后者是 PageViewController。我一直试图在 TNViewController
而不是 TNPViewController
上添加 navigationItem。
这是正确的方法,而且有效。
ParentViewController:
TNPViewController *tnp = [[TNPViewController alloc] initWithNews:news index:indexPath.row];
[[self navigationController] pushViewController:tnp animated:YES];
TNPViewController:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(refreshClicked:)];
self.navigationItem.rightBarButtonItem = refreshButton;
}