uisearch bar width is reduced when change orientat

2019-09-08 08:49发布

I am using tableview with search bar display controller, it shows good in portait and landscape view, but rotate from landscape to portrait search bar size is reduced, how to change the original size

2条回答
forever°为你锁心
2楼-- · 2019-09-08 09:12

Did you try setting the searchBar's frame in the rotation delegate methods or Try auto resizing in XIB.

See this for a better idea.

查看更多
【Aperson】
3楼-- · 2019-09-08 09:35

The first your should catch the orientation:

 //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;

}

Implementation state:

-(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];

  }

}
查看更多
登录 后发表回答