简体   繁体   English

如何在导航栏和UISearchBar之间增加间距?

[英]How can I add spacing between navigation bar and UISearchBar?

I would like to add a progress bar between navigation bar and UISearchBar. 我想在导航栏和UISearchBar之间添加一个进度栏。 May I know how can I implement this? 我可以知道如何实施吗? Please help. 请帮忙。 Thank you. 谢谢。

在此处输入图片说明

Here is my current code in Cell.m 这是我当前在Cell.m中的代码

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGRect barFrame = CGRectInset(self.searchBar.frame, 10.0f, 10.0f);
    self.searchBar.frame = barFrame;
}

Here is my current code in ViewController.m Did not reflect in this code after edited. 这是我在ViewController.m中的当前代码。编辑后未反映在此代码中。 _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];

- (void)viewDidLoad {

    [super viewDidLoad];

    _valueProgress = [[LDProgressView alloc] initWithFrame:CGRectMake(0,DCNaviH, ScreenW, 10.0f)];
    _valueProgress.type = LDProgressSolid;
    _valueProgress.color = ThemeRedColor;
    _valueProgress.progress = 0.40;
    _valueProgress.flat = @YES;
    _valueProgress.showText = @NO;
    [self.view addSubview:_valueProgress];
}
- (UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 24)];
        [_searchBar setBackgroundImage:[UIImage imageNamed:@"ic_searchBar_bgImage"]];
                                                                                                 }];
    }
    return _searchBar;
}

There are a few simple options for you depending on your apps UX. 根据您的应用UX,有一些简单的选项供您选择。 I think the best solution for you based on how you explained your issue would be to include the progress bar to your view and make sure it's above the other views while positioning it below the navigation bar. 我认为,根据您对问题的解释方式,最适合您的解决方案是将进度条包括在视图中,并确保进度条位于其他视图上方,同时将其置于导航栏下方。

_valueProgress = [[LDProgressView alloc] init];
_valueProgress.translatesAutoresizingMaskIntoConstraints = NO;
_valueProgress.type = LDProgressSolid;
_valueProgress.color = ThemeRedColor;
_valueProgress.progress = 0.40;
_valueProgress.flat = @YES;
_valueProgress.showText = @NO;
[self.view addSubview:_valueProgress];
[_valueProgress.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].isActive = YES;
[_valueProgress.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].isActive = YES;
[_valueProgress.heightAnchor constraintEqualToConstant:10.0f].isActive = YES;

if (@available(iOS 11.0, *)) {
    [_valueProgress.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].isActive = YES;
} else {
    [_valueProgress.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].isActive = YES;
}

And if the search bar has been added above the progress bar, you can always call [self.view bringSubviewToFront:_valueProgress] afterwards. 而且,如果已在进度条上方添加了搜索栏,则以后始终可以调用[self.view bringSubviewToFront:_valueProgress]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM