如何在iPad的导航栏中调整自定义UISegmentedControl的大小?

时间:2013-08-07 09:09:33

标签: iphone objective-c uinavigationbar uisegmentedcontrol

我是iPhone应用程序开发的新手。我在导航栏中使用了定制的uisegmentedcontrol。

此代码适用于iPhone。

[self.navigationItem setHidesBackButton:YES animated:YES]; 
    profileSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects: @"seg1", @"seg2", @"seg3", @"seg4", nil]];


[profileSegmentControl addTarget:self action:@selector(seg1Button) forControlEvents:UIControlEventValueChanged];

**profileSegmentControl.frame = CGRectMake(40, 25, 310, 30);**
profileSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
profileSegmentControl.momentary = YES;

//[profileSegmentControl sizeToFit];

UIBarButtonItem *profileSegmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:profileSegmentControl];
self.navigationItem.rightBarButtonItem = profileSegmentBarItem;

现在,当我将方向从纵向更改为横向时,如何在CGRectMake中更改uisegmentedcontroll按钮的坐标?在横向时,宽度应为510而不是310。

无法使用[segmentControlObject sizeToFit],因为我隐藏了导航栏的“后退”导航按钮。我尝试过这个,但是segmentedControl的宽度并未覆盖整个导航栏。

我知道(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation方法,但你不能在viewDidLoad或任何方法下调用它。

使用xcode 4.6

1 个答案:

答案 0 :(得分:0)

Do like this
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){
      FACING = @"PU";
  }
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){
      FACING = @"PD";
  }
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
      FACING = @"LL";
  }
  if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){
      FACING = @"LR";
  } 

所以在相应的块中设置任何你想要的帧。它可以工作。