UISearchBar宽度错误的景观

And*_*y A 4 iphone cocoa-touch landscape orientation uisearchbar

当您以横向模式启动时,我的搜索栏会稍微向右拉伸.如果然后旋转到纵向模式,它仍然略宽.但是,如果您以纵向模式启动,并且然后将其旋转为横向,则它很好.这是我的代码.

sBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[sBar sizeToFit];
sBar.delegate = self;
sBar.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:sBar];
Run Code Online (Sandbox Code Playgroud)

And*_*y A 6

答案是在运行其余代码之前将搜索栏添加到视图中.

UISearchBar * tempSBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.sBar = tempSBar;
[tempSBar release];

[self.view addSubview:sBar];
[sBar sizeToFit];
sBar.delegate = self;
sBar.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin;
Run Code Online (Sandbox Code Playgroud)