将活动指标纵向和中心对齐横向Xcode

时间:2013-11-28 09:19:14

标签: iphone objective-c ios7 xcode5

目前在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,addviewcontroller和detailviewcontroller中设置我的活动指示器在ViewdidLoad中

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

以下代码是我用来在我的rootviewcontroller中的ViewDidLoad中调用的Activity Indicator的中心。

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

非常感谢任何帮助

5 个答案:

答案 0 :(得分: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];

答案 1 :(得分:1)

spinner.center = self.view.center;

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

试试这个:

   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 :(得分:0)

- (void)viewDidLayoutSubviews {    
    self.activityIndicator.center = self.view.center; 
}

试试这个肖像