UIView Programmatic Width Constraint - ios

What kind of constraint(s) do I need to add in order to ensure when the phone rotates the search bar and label span the width of the phone?
Below is code I am using to spin up a search bar & label and add them to a table header:
self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.venueSearchController.searchResultsUpdater = self;
self.venueSearchController.searchBar.delegate = self;
self.venueSearchController.delegate = self;
self.venueSearchController.dimsBackgroundDuringPresentation = NO;
self.venueSearchController.hidesNavigationBarDuringPresentation = YES;
self.venueSearchController.searchBar.frame = CGRectMake(0, 0, self.venueSearchController.view.frame.size.width, 44.0);
// create a UISegmentControl
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, self.venueSearchController.view.frame.size.width, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Text Here";
tableHeaderLabel.backgroundColor = [UIColor redColor];
// create a custom UIView
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, 74)];
[myView addSubview:tableHeaderLabel]; // add header label
[myView addSubview:self.venueSearchController.searchBar]; // add search bar to custom view
self.tableView.tableHeaderView = myView;

FYI I fixed this by simply using autoresizingMask
tableHeaderLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.venueSearchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Related

Searchbar in tableView and reload data

I can not search because my search bar is a header of tableView and then I reload tableView I have not search results.
I know there is a lot of ways to solve it but what is more wisely?
My code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.searchBar.delegate = self;
view.opaque = NO;
view.backgroundColor = [UIColor clearColor];
self.searchBar.opaque = NO;
self.searchBar.translucent = NO;
self.searchBar.backgroundColor = [UIColor clearColor];
[view addSubview: self.searchBar];
self.searchBar.barStyle = UISearchBarStyleDefault;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ;
self.searchBar.barTintColor = [UIColor clearColor];
return view;
}
And I want that then I scroll tableView my searchBar will disappear in top and then I scroll up it will appear. Is any simple to do it?
You need to use tableHeaderView and not section header. Just put in your viewDidLoad
UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.tableView.tableHeaderView = searchBar;

Adding subviews to tableHeaderView with UISearchBar in iOS7 misbehaves

How can one add two simple UILabels to a TableHeaderView and retain the default searchbar behavior of the search display controller in iOS7?
Or, visually:
Why does this code:
UIView *tableHeadView = self.tableView.tableHeaderView;
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 36, 320, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Questions"
UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 320, 30)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = #"test test test test";
[tableHeadView addSubview:tableHeaderLabel];
[tableHeadView addSubview:tableHeaderPrononce];
added to a UITableViewController viewDidLoad event (which contains a UISearchDisplayerController)
gives this lovely result in iOS6:
and this horrible terrible result in iOS7:
The behavior:
During normal mode the UILabels I added are not shown. while search is active the UILabels suddenly appear ON TOP OF the table cells and does not scroll away
In addition I am getting crashes while searching in iOS 7 which never occurred on iOS6. Probably not related to this piece of code but nevertheless I should mentioned that.
I tried all I could find on SO about fixing this issue but always something else breaks or disappears (mainly the UISearchBar).
Help
Look dude, before you modify your code or get frustrated with the behavior of the iOS7, it is advised to go through the UI Transition Guide given by apple.
After reading this you will get to know, why the grouped style tableView is looking annoying is not at all annoying from ios7 point of view. Each group extends the full width of the screen.
Moreover the UISearchBar behavior can be controlled if you read through Search Bar and Scope Bar
I hope that helps. Should you need all the code, please let us know. we can provide sample.
Here is Sample Code as Required Adding UISearchBar to View rather than TableViewHeader
Adding UISearchBar to self.view
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 44.0f)];
searchBar.delegate = self;
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
[self.view addSubview:searchBar];
Adding UITableView to self.view
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tableView];
Creating and Adding UITableView.tableHeaderView
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 15)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Questions";
tableHeaderLabel.alpha = 0.9;
UILabel *tableHeaderPrononce = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 320, 15)];
[tableHeaderPrononce setTextAlignment:NSTextAlignmentCenter];
tableHeaderPrononce.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
tableHeaderPrononce.text = #"test test test test";
tableHeaderPrononce.alpha = 0.7;
UIView *aHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[aHeaderView setBackgroundColor:[UIColor clearColor]];
[aHeaderView addSubview:tableHeaderLabel];
[aHeaderView addSubview:tableHeaderPrononce];
tableView.tableHeaderView = aHeaderView;
Here are the results:
SearchDisplayController
// Create search controller
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;
You can do this simply by UITableViewDelegate method shown below:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
sectionHeaderView.backgroundColor = [UIColor grayColor];
UILabel *headerLabel = [[UILabel alloc] init];
[sectionHeaderView addSubview:headerLabel];
return sectionHeaderView;
}

Issue with "autoresizingMask" with two UITextViews

I have a problem with "autoresizingMask" with two UITextView. When I resize the two UITextViews, overlaps the first above the second.
I attached an image to explain the problem.
Thanks!
Update: I put the revenant code
[UIView]
self.autoresizesSubviews = YES;
// Configure title label
_titleLabel = [[UILabel alloc] init];
_titleLabel.frame = CGRectMake(4.0, 4.0, self.bounds.size.width - 8.0, 33.0);
_titleLabel.backgroundColor = APP_COLOR(1.0);
_titleLabel.font = FONT_LATO_BOLD(18.0);
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_titleLabel];
// Question textView
_questionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(_titleLabel.bounds), (usableHeight / 2.0) - 4.0)];
_questionTextView.layer.cornerRadius = 8.0;
_questionTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
[self addSubview:_questionTextView];
// Answer textView
_answerTextView = [[UITextView alloc] initWithFrame:CGRectMake(CGRectGetMinX(_questionTextView.frame), CGRectGetMaxY(_questionTextView.frame) + 8.0, CGRectGetWidth(_questionTextView.bounds), (usableHeight / 2.0) - 4.0)];
_answerTextView.layer.cornerRadius = 8.0;
_answerTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
[self addSubview:_answerTextView];
The autoresize is working fine.
The two views just don't fit together in the parent view.
You have to adjust the view height manually.

Auto position subviews of UINavigationBar's titleView

I have a custom titleView for displaying Navigation title and sub-title.
The title font is bigger than sub-title.
Here's what I have now:
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 0, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = #"Title";
titleView.adjustsFontSizeToFitWidth = YES;
CGRect subtitleFrame = CGRectMake(0, 24, 200, 20);
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = UITextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = #"subtitle";
subtitleView.adjustsFontSizeToFitWidth = YES;
_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[_headerTitleSubtitleView addSubview:titleView];
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
I want to have the ability to remove subTitleView at some point in my code,
but that makes the titleView look out of center (vertically).
So my question is, how would you implement auto-positioning here?
should I use layoutSubviews ?
if so, what would be the proper way to override it?
I thought that when the container view has an autoresizingMask it will adjust it's size according to the "total" size of it's subviews (?).
Thanks
I was able to resolve this issue by creating a separate view class and
implement layoutSubviews.
Title label subview is always attached.
Subtitle label subview can be removed/added whenever need - which
will call layoutSubViews to make re-position the Title label view in center.
- (id)initWithFrame:(CGRect)frame
{
CGRect containerFrame = CGRectMake(0, 0, 200, 44);
self = [super initWithFrame:containerFrame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.autoresizesSubviews = YES;
_title = [[UILabel alloc] initWithFrame:CGRectZero];
_title.backgroundColor = [UIColor clearColor];
_title.font = [UIFont boldSystemFontOfSize:20];
_title.textAlignment = UITextAlignmentCenter;
_title.textColor = [UIColor whiteColor];
_title.shadowColor = [UIColor darkGrayColor];
_title.shadowOffset = CGSizeMake(0, -1);
_title.adjustsFontSizeToFitWidth = YES;
_subTitle = [[UILabel alloc] initWithFrame:CGRectZero];
_subTitle.backgroundColor = [UIColor clearColor];
_subTitle.font = [UIFont boldSystemFontOfSize:13];
_subTitle.textAlignment = UITextAlignmentCenter;
_subTitle.textColor = [UIColor whiteColor];
_subTitle.shadowColor = [UIColor darkGrayColor];
_subTitle.shadowOffset = CGSizeMake(0, -1);
_subTitle.adjustsFontSizeToFitWidth = YES;
self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[self addSubview:_title];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (([self.subviews count] > 1) && ([[self.subviews objectAtIndex:1] isKindOfClass:[UILabel class]]))
{
[[self.subviews objectAtIndex:0] setFrame:CGRectMake(0,0,200,24)];
[[self.subviews objectAtIndex:1] setFrame:CGRectMake(0,24,200,20)];
}
else
[[self.subviews objectAtIndex:0] setFrame:self.bounds];
}

iPad - Autoresizing for UITableView

My Code is Here :
CGRect mainFrame = [UIScreen mainScreen].bounds;
_contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0.0, mainFrame.size.width, mainFrame.size.height-20)] autorelease];
_contentView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
_contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_contentView];
self.tableView = [[[UITableView alloc] initWithFrame:_contentView.frame style:UITableViewStylePlain] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.tableView setSeparatorColor:[UIColor clearColor]];
[_contentView addSubview:_tableView];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
I add a tableview on _contentView and _contentView on self.view
on iPhone works good without problem, but on iPad I don't know why when I set autoresizing mask for table view, it increases width and height... without autoresizing mask works good, but I need autoresizing mask for landscape. How to solve this ?
with autoresizing mask
without autoresizing mask
Once try this
_contentView.autoresizesSubviews = YES ;
Hope this will work.

Resources