当从横向更改为纵向时,uisearchbar宽度会减小

时间:2013-05-02 04:48:47

标签: ios uitableview ios6 uisearchbar

我正在使用带有搜索栏显示控制器的tableview,它在portait和landscape视图中显示效果很好,但是从横向旋转到纵向搜索栏尺寸减小了,如何更改原始尺寸

2 个答案:

答案 0 :(得分:0)

您是否尝试在轮播委托方法中设置searchBar的框架或尝试在XIB中自动调整大小。

有关更好的建议,请参阅this

答案 1 :(得分:0)

第一个你应该抓住方向:

 //AppDelegate.h
 @interface AppDelegate : UIResponder <UIApplicationDelegate>
 @property (nonatomic, unsafe_unretained) NSInteger isPortrait;

//AppDelegate.m
@synthesize isPortrait;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

   isPortrait = 1;

}

实施状态:

-(void)viewWillAppear:(BOOL)animated{

   [[UIApplication sharedApplication] statusBarOrientation];
   [[UIDevice currentDevice] orientation];
   [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

 //methode deviceRotated 

-(void)deviceRotated:(NSNotification*)notification{

   AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
   UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
   if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
   {
      [self searchBarLandscape];
       app.isPortrait = NO;
  }
  else{
     [self searchBarPortrait];
     app.isPortrait = YES;
  }

}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
   if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || toInterfaceOrientation == UIInterfaceOrientationPortrait) 
   {
    app.isPortrait = 1;
   }
  else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
  {
    app.isPortrait = 0;
  }
  [self prepareSearchBarOrientation];

}


-(void)prepareSearchBarOrientation
{

   AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
   if (app.isPortrait)
   {
       [self searchBarPortrait];//Set Frame here
   }
  else
  {
      [self searchBarLandscape];

  }

}