UISearchBar width wrong in landscape
My search bar is stretched slightly too far right开发者_StackOverflow中文版 when you start in landscape mode. It is still slightly too wide if you then rotate to portrait mode. However, its fine if you start in portrait mode and also if you then rotate it to landscape. Here is my code.
sBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[sBar sizeToFit];
sBar.delegate = self;
sBar.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:sBar];
The answer was to add the search bar to the view before running the rest of the code.
UISearchBar * tempSBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.sBar = tempSBar;
[tempSBar release];
[self.view addSubview:sBar];
[sBar sizeToFit];
sBar.delegate = self;
sBar.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin;
UIViewAutoresizingFlexibleRightMargin
add that to sBar.autoresizingMask, i have that aswell as the other 2 and have no issues. if you are using IB, make sure its the right width in there also
I know this is old and deprecated, but I just had to share. I looked high and low for a solution that would work for my case. I tried all the suggestions, to no avail. Then I stumbled across this gem
// Update search bar frame.
CGRect superviewFrame = self.searchDisplayController.searchBar.superview.frame;
superviewFrame.origin.y = 0.f;
self.searchDisplayController.searchBar.superview.frame = superviewFrame;
And everything became nice and sunny again. Thank you Peter at http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/
精彩评论