3 回答

TA貢獻1796條經(jīng)驗 獲得超4個贊
這是一個簡單的效果。
首先,將分段放置在工具欄中。將此工具欄放在導航欄的正下方。設置工具欄視圖控制器的代表,并返回UIBarPositionTopAttached
在positionForBar:
。您可以在商店應用程序中看到,如果執(zhí)行交互式彈出手勢,則分段欄的移動與導航欄的移動不同。那是因為他們不一樣。
現(xiàn)在刪除發(fā)際線。“發(fā)際線”是UIImageView
導航欄的子視圖。您可以找到它并將其設置為隱藏。例如,這就是Apple在其本機日歷應用程序以及商店應用程序中所做的。請記住在當前視圖消失時顯示它。如果您稍稍玩一下Apple應用程序,將會看到發(fā)際線設置為隱藏在上,viewWillAppear:
并顯示為viewDidDisappear:
。
要獲得搜索欄的樣式,只需將其設置為searchBarStyle
即可UISearchBarStyleMinimal
。

TA貢獻1818條經(jīng)驗 獲得超11個贊
現(xiàn)在刪除發(fā)際線?!凹毦€”是UIImageView,它是導航欄的子視圖。您可以找到它并將其設置為隱藏。例如,這就是Apple在其本機日歷應用程序以及商店應用程序中所做的。請記住在當前視圖消失時顯示它。如果您稍微玩一下Apple應用程序,將會看到發(fā)際線在viewWillAppear:上設置為隱藏,并在viewDidDisappear:上顯示。
另一種方法是查找發(fā)際線并將其移至添加的工具欄下方。這是我想出的。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *segmentbar;
@property (weak, nonatomic) UIImageView *navHairline;
@end
@implementation ViewController
#pragma mark - View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// find the hairline below the navigationBar
for (UIView *aView in self.navigationController.navigationBar.subviews) {
for (UIView *bView in aView.subviews) {
if ([bView isKindOfClass:[UIImageView class]] &&
bView.bounds.size.width == self.navigationController.navigationBar.frame.size.width &&
bView.bounds.size.height < 2) {
self.navHairline = (UIImageView *)bView;
}
}
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self _moveHairline:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self _moveHairline:NO];
}
- (void)_moveHairline:(BOOL)appearing
{
// move the hairline below the segmentbar
CGRect hairlineFrame = self.navHairline.frame;
if (appearing) {
hairlineFrame.origin.y += self.segmentbar.bounds.size.height;
} else {
hairlineFrame.origin.y -= self.segmentbar.bounds.size.height;
}
self.navHairline.frame = hairlineFrame;
}
@end
我還發(fā)現(xiàn)Apple NavBar代碼示例(自定義UINavigationBar)對于解決此問題非常有用。
另外,請確保處理UIToolbar的頂部邊框,它可能會出現(xiàn),并且您可能會將其與NavBar的細線混淆。我還希望UIToolbar看起來與NavBar完全一樣,然后您可能想要調整ToolbarsbarTintColor。

TA貢獻1848條經(jīng)驗 獲得超10個贊
這是針對此特定問題的面向協(xié)議的Swift方法,基于公認的答案:
HideableHairlineViewController.swift
protocol HideableHairlineViewController {
func hideHairline()
func showHairline()
}
extension HideableHairlineViewController where Self: UIViewController {
func hideHairline() {
findHairline()?.hidden = true
}
func showHairline() {
findHairline()?.hidden = false
}
private func findHairline() -> UIImageView? {
return navigationController?.navigationBar.subviews
.flatMap { $0.subviews }
.flatMap { $0 as? UIImageView }
.filter { $0.bounds.size.width == self.navigationController?.navigationBar.bounds.size.width }
.filter { $0.bounds.size.height <= 2 }
.first
}
}
SampleViewController.swift
import UIKit
class SampleViewController: UIViewController, HideableHairlineViewController {
@IBOutlet private weak var toolbar: UIToolbar!
@IBOutlet private weak var segmentedControl: UISegmentedControl!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
hideHairline()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
showHairline()
}
}
// MARK: UIToolbarDelegate
extension SampleViewController: UIToolbarDelegate {
func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
return .TopAttached
}
}
- 3 回答
- 0 關注
- 862 瀏覽
添加回答
舉報