【问题标题】:Centering the Activity Indicator Portrait & Landscape Xcode居中活动指示器纵向和横向 Xcode
【发布时间】:2013-11-28 09:19:13
【问题描述】:

目前在 IOS 7 下工作,使用下面的代码,我能够在我的 rootviewcontroller 中将活动指示器置于横向和纵向的中心。但是,该通知不会在我的 addviewcontroller 或我的同一项目的 detailviewcontroller 中触发。我希望能够在我的 addviewcontroller 和 detailviewcontroller 中将活动指示器居中,就像我为 rootviewcontroller 所做的那样。

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

//Configure and show the window
[window addSubview:[navigationController view]];

[window makeKeyAndVisible];

//Applications are expected to have root view controller at the end of application launch
self.window.rootViewController = navigationController;

return YES;
}

以下代码说明了我如何在我的 rootviewcontroller 中设置我的 Activity Indicator,在 ViewdidLoad 中添加viewcontroller 和 detailviewcontroller

 //Setup Activity Indicator.
self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
self.activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.activityIndicator.transform = CGAffineTransformMakeScale(1.35, 1.35);
[self.view addSubview:self.activityIndicator];

下面的代码是我用来在我的根视图控制器的 ViewDidLoad 中调用的活动指示器居中的。

//Make sure the activity indicator is centered both in landscape and portrait
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

-(void)didRotate:(NSNotification *)notification {
   if (activityIndicator) {
       activityIndicator.center = CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);
    }
 }

非常感谢任何帮助

【问题讨论】:

    标签: iphone objective-c ios7 xcode5


    【解决方案1】:

    尝试为纵向和横向模式提供单独的框架。

    UIActivityIndicatorView *activityView;
    
     activityView = [[UIActivityIndicatorView alloc] init];
    
    if(isPortraitMode)
                [activityView setFrame:CGRectMake(147.5,227.5,25,25)];
            else
                [activityView setFrame:CGRectMake(227.5,147.5,25,25)];
    
    [self.view addSubview:activityView];
    

    【讨论】:

      【解决方案2】:

      spinner.center = self.view.center;

      【讨论】:

        【解决方案3】:

        试试这个:

           self.activityIndicator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);// if you want whole screen center then used :[UIScreen mainScreen].bounds
        

        快乐编码...:)

        【讨论】:

          【解决方案4】:

          我认为处理旋转事件更好的地方是 viewDidLayoutSubviews 方法,而不是通知中心。 我会将此添加到您想要居中的每个控制器中:

          -(void)viewDidLayoutSubviews
          {
              activityIndicator.center = CGPointMake(self.view.bounds.size.width/2,self.view.bounds.size.height/2);
          }
          

          【讨论】:

          • 是的,格雷格,这是我目前正在尝试的方法,它有效。但是,当我从纵向旋转到横向时,它似乎并没有重新定位活动指示器,因为它确实为上面的 rootviewcontroller 实现了通知方法。
          • -(void)viewDidLayoutSubviews { self.activityIndi​​cator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);这个解决方案非常适合我
          【解决方案5】:
          - (void)viewDidLayoutSubviews {    
              self.activityIndicator.center = self.view.center; 
          }
          

          试试这幅肖像

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-23
            • 2015-10-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多