ios - UIsearchController inside UIViewController using Auto Layout -
has been successful implementing uiviewcontroller
contais both uisearchcontroller
searchbar
, uitableview
while laying out using auto layout?
i'm trying achieve similar 1password on iphone: fixed searchbar
on top of tableview
(not part of tableheaderview
). when uisearchcontroller
owns searchbar
gets activated, searchbar
animates show scope buttons , tableview
moves down bit.
i have got basics of layout working correctly class:
// // viewcontroller.m // uisearchcontrollerissues // // created aloha silver on 05/02/16. // copyright © 2016 abc. rights reserved. // #import "viewcontroller.h" @interface viewcontroller () <uisearchresultsupdating, uitableviewdatasource, uitableviewdelegate> @property (nonatomic, strong) uisearchcontroller *searchcontroller; @property (nonatomic, strong) uitableview *tableview; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; [self setuptableview]; [self setupsearchinterface]; [self setupconstraints]; self.edgesforextendedlayout = uirectedgenone; self.extendedlayoutincludesopaquebars = yes; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; [self.searchcontroller.searchbar sizetofit]; } - (void)setuptableview { self.tableview = [[uitableview alloc] initwithframe:cgrectzero style:uitableviewstylegrouped]; self.tableview.datasource = self; self.tableview.delegate = self; self.tableview.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:self.tableview]; } - (void)setupsearchinterface { self.definespresentationcontext = yes; self.searchcontroller = [[uisearchcontroller alloc] initwithsearchresultscontroller:nil]; self.searchcontroller.dimsbackgroundduringpresentation = no; self.searchcontroller.hidesnavigationbarduringpresentation = no; self.searchcontroller.searchbar.scopebuttontitles = @[@"one", @"two"]; self.searchcontroller.searchbar.translatesautoresizingmaskintoconstraints = no; self.searchcontroller.searchresultsupdater = self; [self.view addsubview:self.searchcontroller.searchbar]; } - (void)setupconstraints { nsdictionary *layoutviews = @{@"searchbar": self.searchcontroller.searchbar, @"tableview": self.tableview, @"toplayoutguide": self.toplayoutguide}; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[searchbar]|" options:0 metrics:nil views:layoutviews]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[tableview]|" options:0 metrics:nil views:layoutviews]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[searchbar(44)][tableview]|" options:0 metrics:nil views:layoutviews]]; } - (void)updatesearchresultsforsearchcontroller:(uisearchcontroller *)searchcontroller { nslog(@"update should happen here"); } #pragma mark - uitableviewdatasource - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 100; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellid = @"cellid"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellid]; if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } cell.textlabel.text = [nsstring stringwithformat:@"cell number %ld", (long)indexpath.row]; return cell; } @end
it embedded in uinavigationcontroller
instance , runs expect, following screenshots show:
trouble arises when searchbar
gets activated. seems disappear screen, after inspecting view, determine onscreen, width
of zero. here's picture showing presented @ time:
i'm not experienced auto layout, i'm left thinking there must wrong constraints, although don't mess them when activating uisearchcontroller
.
is there way of making work?
the uisearchcontroller moves search bar around when it's tapped, doesn't play constraints.
instead of setting constraints directly on search bar, add empty placeholder view hold search bar , place search bar in procedurally in viewdidload()
. set constraints on placeholder instead. match search bar's frame placeholder's bounds , leave translatesautoresizingmaskintoconstraints
set true.
sorry, i'm not sure how placeholder view handle size change scope buttons. can figure out how part working auto layout after change.
Comments
Post a Comment