使用自定义WebView和UISearchContoller创建自定义UIView

时间:2016-06-27 00:05:58

标签: ios objective-c uiview uiwebview uisearchcontroller

我正在开发一个iOS应用程序,我创建了一个UIViewController,我放置了我的组件,它运行正常。 现在,我正在尝试创建自定义UIView并将WebViewSearchController放入其中。我花了很多时间没有成功。

这是我的.m文件,我希望有人可以帮助我:

#import "HomeViewController.h"


#define widthtScreen  [UIScreen mainScreen].bounds.size.width
#define heightScreen  [UIScreen mainScreen].bounds.size.height


@interface HomeViewController () <UISearchResultsUpdating,UISearchBarDelegate,UIBarPositioningDelegate,UITableViewDataSource,UITableViewDelegate,MapWebViewDelegate>

@property(strong,nonatomic) MapWebView *webView;

@property (nonatomic) UIButton  *btnGeolocate;


@property (nonatomic, strong) UISearchController *searchController;

@end

@implementation HomeViewController{
    NSMutableArray *placesList;
    BOOL isSearching;
}



-(void)loadView
{
    [super loadView];
    self.webView = [[MapWebView alloc] initWithFrame:CGRectMake(0, -100, widthtScreen, heightScreen+100)];
    self.webView.mapWebViewDelegate = self;
    [self.view addSubview:self.webView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton = YES;
    mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    placesList = [[NSMutableArray alloc] init];
    [self initializeSearchController];
    mainDelegate.webView = self.webView;


    self.btnGeolocate = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-75,550,60,60)];
    self.btnGeolocate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.btnGeolocate setBackgroundImage:[UIImage imageNamed:@"geo.png"]
                                 forState:UIControlStateNormal];
    [self.btnGeolocate addTarget:self action:@selector(btnGeolocatePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnGeolocate];

    mainDelegate.btnZoomIn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80,620,30,30)];
    mainDelegate.btnZoomIn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [mainDelegate.btnZoomIn setBackgroundColor:[UIColor blackColor]];
    [mainDelegate.btnZoomIn addTarget:self action:@selector(btnZoomInPressed:) forControlEvents:UIControlEventTouchUpInside];
    mainDelegate.btnZoomIn.tag=1;
    UIImage *btnImage = [UIImage imageNamed:@"plus.png"];
    [mainDelegate.btnZoomIn setImage:btnImage forState:UIControlStateNormal];
    [self.view addSubview:mainDelegate.btnZoomIn];

    mainDelegate.btnZoomOut = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-40,620,30,30)];
    mainDelegate.btnZoomOut.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [mainDelegate.btnZoomOut setBackgroundColor:[UIColor blackColor]];
    [mainDelegate.btnZoomOut addTarget:self action:@selector(btnZoomOutPressed:) forControlEvents:UIControlEventTouchUpInside];
    mainDelegate.btnZoomOut.tag=1;
    UIImage *btnImage2 = [UIImage imageNamed:@"minus.png"];
    [mainDelegate.btnZoomOut setImage:btnImage2 forState:UIControlStateNormal];
    [self.view addSubview:mainDelegate.btnZoomOut];
}

- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
{
    return YES;
}

- (BOOL)slideNavigationControllerShouldDisplayRightMenu
{
    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"number :%lu",(unsigned long)[placesList count]);
    return [placesList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    SuggestResultObject *sro = [SuggestResultObject new];
    sro = [placesList objectAtIndex:indexPath.row];
    cell.textLabel.text = sro.textPlace;
    return cell;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem =nil;
    return true;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.navigationItem.leftBarButtonItem = mainDelegate.leftBarButtonItem;
    self.navigationItem.rightBarButtonItem = mainDelegate.rightBarButtonItem;
    SuggestResultObject *sro = [SuggestResultObject new];
    sro = [placesList objectAtIndex:indexPath.row];
    self.searchController.active = false;
    NSString *function = [[NSString alloc] initWithFormat: @"MobileManager.getInstance().moveToLocation(\"%@\",\"%@\")", sro.latPlace,sro.lonPlace];
    [_webView evaluateJavaScript:function completionHandler:nil];
}



- (void)jsRun:(NSString *) searchText {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *function = [[NSString alloc] initWithFormat: @"MobileManager.getInstance().setSuggest(\"%@\")", searchText];
        [_webView evaluateJavaScript:function completionHandler:nil];
    });
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearching = YES;
}

- (void)initializeSearchController {
    UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    searchResultsController.tableView.dataSource = self;
    searchResultsController.tableView.delegate = self;
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    self.definesPresentationContext = YES;
    self.searchController.hidesNavigationBarDuringPresentation = false;
    self.searchController.accessibilityElementsHidden= true;
    self.searchController.dimsBackgroundDuringPresentation = true;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    self.navigationItem.titleView = self.searchController.searchBar;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    self.navigationItem.leftBarButtonItem = mainDelegate.leftBarButtonItem;
    self.navigationItem.rightBarButtonItem = mainDelegate.rightBarButtonItem;
    NSLog(@"Cancel clicked");
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [placesList removeAllObjects];
}


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    [placesList removeAllObjects];
    if([searchController.searchBar.text length] != 0) {
        isSearching = YES;
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
        [self jsRun:searchController.searchBar.text];
    }
    else {
        isSearching = NO;
        [((UITableViewController *)self.searchController.searchResultsController).tableView reloadData];
    }
}

-(void) btnGeolocatePressed : (id) sender{
}

-(void) btnZoomInPressed : (id) sender{
    [_webView evaluateJavaScript:@"MobileManager.getInstance().zoomIn();" completionHandler:nil];
}

-(void) btnZoomOutPressed : (id) sender{
    [_webView evaluateJavaScript:@"MobileManager.getInstance().zoomOut();" completionHandler:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)searchResult:(NSArray*)dataArray{
    SuggestResultObject *sro = [SuggestResultObject new];
    sro.textPlace = [dataArray objectAtIndex:0];
    sro.lonPlace = [dataArray objectAtIndex:1];
    sro.latPlace = [dataArray objectAtIndex:2];
    [placesList addObject:sro];
    [((UITableViewController *)self.searchController.searchResultsController).tableView reloadData];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
@end

1 个答案:

答案 0 :(得分:0)

截图(不确定如何在stackoverflow上缩放图像.. OSX以Retina格式拍摄图像,它们相当大!提前抱歉!):

http://i.imgur.com/15qxDpc.png

http://i.imgur.com/QHduP07.png

  

它是如何工作的?创建UIView和两个子视图:UITableView和   UIWebView。正确地约束它们。

     

使用UISearchController SearchResultsController创建nil   作为init方法的参数。

     

这使搜索控制器知道结果将是   显示在当前控制器/视图中。

     

接下来,我们为UISearchController设置委托并创建   过滤功能。

     

现在我们已经创建了视图,我们需要UIViewController来   测试一下。我们可以将搜索栏添加到tableView标题OR   如果有一个导航控制器..

     

下面,我选择将其添加到UINavigationController和我   告诉UISearchController不要隐藏导航栏   介绍

     

这样,结果显示在当前视图中而不隐藏   导航栏。

     

然后,您可以使用隐藏和屏幕外的webview进行操作   无论你使用它进行javascript搜索..

     

然而,更好的想法是使用JSContext来执行   Javascript而不是UIWebViewUIWebView的优势   是你可以解析HTML并修改JSContext的DOM   不允许。

     

反正..

     

以下是我为包含a的UIView编写的代码   UISearchControllerUIWebView ..然后将其添加到   嵌入在UIViewController

中的UINavigationController
//
//  SearchView.h
//  StackOverflow
//
//  Created by Brandon T on 2016-06-26.
//  Copyright © 2016 XIO. All rights reserved.
//

#import <UIKit/UIKit.h>

@class SearchView;

@protocol SearchViewDelegate <UISearchBarDelegate>
- (void)didSelectRowAtIndexPath:(SearchView *)searchView tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath;
@end

@interface SearchView : UIView
@property (nonatomic, weak) id<SearchViewDelegate> delegate;
- (UISearchBar *)getSearchBar;
- (UIWebView *)getWebView;
@end


//
//  SearchView.m
//  StackOverflow
//
//  Created by Brandon T on 2016-06-26.
//  Copyright © 2016 XIO. All rights reserved.
//

#import "SearchView.h"

#define kTableViewCellIdentifier @"kTableViewCellIdentifier"

@interface SearchView() <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSArray *searchResults;
@end

@implementation SearchView

- (instancetype)init {
    if (self = [super init]) {

        [self setupData];
        [self initControls];
        [self themeControls];
        [self registerCells];
        [self doLayout];
    }
    return self;
}

- (UISearchBar *)getSearchBar {
    return _searchController.searchBar;
}

- (UIWebView *)getWebView {
    return _webView;
}

- (void)setDelegate:(id<SearchViewDelegate>)delegate {
    _delegate = delegate;
    _searchController.searchBar.delegate = delegate;
}

- (void)setupData {
    //Begin fake data

    _dataSource = @[@"Cat", @"Dog", @"Bird", @"Parrot", @"Rabbit", @"Racoon", @"Rat", @"Hamster", @"Pig", @"Cow"];

    //End fake data


    _searchResults = [_dataSource copy];
}

- (void)initControls {
    _webView = [[UIWebView alloc] init];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
}

- (void)themeControls {
    [_webView setHidden:YES];

    [_tableView setDelegate:self];
    [_tableView setDataSource:self];

    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = false;
    _searchController.definesPresentationContext = true;
    _searchController.hidesNavigationBarDuringPresentation = false;
}

- (void)registerCells {
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellIdentifier];
}

- (void)doLayout {
    [self addSubview:_webView];
    [self addSubview:_tableView];

    NSDictionary *views = @{@"webView":_webView, @"tableView": _tableView};
    NSMutableArray *constraints = [[NSMutableArray alloc] init];

    [constraints addObject:[NSString stringWithFormat:@"H:|-(%d)-[webView]-(%d)-|", 0, 0]];
    [constraints addObject:[NSString stringWithFormat:@"H:|-(%d)-[tableView]-(%d)-|", 0, 0]];

    [constraints addObject:[NSString stringWithFormat:@"V:|-(%d)-[webView(%d)]-(%d)-[tableView]-(%d)-|", -100, 100, 0, 0]];

    for (NSString *constraint in constraints) {
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
    }

    for (UIView *view in self.subviews) {
        [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchController.active && _searchController.searchBar.text.length > 0 ? [_searchResults count] : [_dataSource count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier forIndexPath:indexPath];

    if (_searchController.active && _searchController.searchBar.text.length > 0) {
        cell.textLabel.text = _searchResults[indexPath.row];
    }
    else {
        cell.textLabel.text = _dataSource[indexPath.row];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectRowAtIndexPath:tableView:indexPath:)]) {
        [self.delegate didSelectRowAtIndexPath:self tableView:tableView indexPath:indexPath];
    }
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    [self filterResults:searchController.searchBar.text scope:nil];
}

- (void)filterResults:(NSString *)searchText scope:(NSString *)scope {
    _searchResults = [_dataSource filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {

        NSString *object = [evaluatedObject uppercaseString];
        return [object rangeOfString:[searchText uppercaseString]].location != NSNotFound;
    }]];

    [_tableView reloadData];
}

@end

然后我使用嵌入UIViewController中的UINavigationController进行了测试..

//
//  ViewController.m
//  StackOverflow
//
//  Created by Brandon T on 2016-06-26.
//  Copyright © 2016 XIO. All rights reserved.
//

#import "ViewController.h"
#import "SearchView.h"


@interface ViewController ()<SearchViewDelegate>
@property (nonatomic, strong) SearchView *searchView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initControls];
    [self themeControls];
    [self doLayout];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)initControls {
    _searchView = [[SearchView alloc] init];
}

- (void)themeControls {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.titleView = [_searchView getSearchBar];

    [_searchView setDelegate:self];
}

- (void)doLayout {
    [self.view addSubview:_searchView];

    NSDictionary *views = @{@"searchView":_searchView};
    NSMutableArray *constraints = [[NSMutableArray alloc] init];

    [constraints addObject:[NSString stringWithFormat:@"H:|-%d-[searchView]-%d-|", 0, 0]];
    [constraints addObject:[NSString stringWithFormat:@"V:|-%d-[searchView]-%d-|", 0, 0]];

    for (NSString *constraint in constraints) {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
    }

    for (UIView *view in self.view.subviews) {
        [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    self.navigationItem.leftBarButtonItems = nil;
    self.navigationItem.rightBarButtonItems = nil;
}

- (void)didSelectRowAtIndexPath:(SearchView *)searchView tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {

    [[searchView getWebView] stringByEvaluatingJavaScriptFromString:@"SomeJavascriptHere"];

}
@end