我有一个自定义的alterviewcontroller,它是UIViewController的子类。它在肖像应用程序中运行良好,但在横向上,它会遇到框架方向问题。我尝试了很多修复,但似乎都没有做到。
在当前版本中,它显示正确,但主视图的大小不正确,因此该视图外部的按钮无法点击。
在此图像中,“下一个转弯”按钮不是“可点击”,“转到黑色”按钮是。这是景观。
.h文件
#import <UIKit/UIKit.h> #import "SPRTitleFontLabel.h" @protocol SPRAlertViewControllerDelegate; @interface SPRAlertViewController : UIViewController @property (nonatomic, weak) id <SPRAlertViewControllerDelegate> delegate; @property (weak, nonatomic) IBOutlet UIView *alertview; @property (weak, nonatomic) IBOutlet SPRTitleFontLabel *titleLabel; @property (weak, nonatomic) IBOutlet UITextView *messageLabel; @property (weak, nonatomic) IBOutlet UIButton *yesButton; @property (weak, nonatomic) IBOutlet UIButton *noButton; @property (weak, nonatomic) IBOutlet UIButton *okButton; @property int tag; -(void)setTitle:(NSString *)title message:(NSString *)message andButtonCount:(int)buttonCount; - (IBAction)doDismiss:(id)sender; - (void)show; @end @protocol SPRAlertViewControllerDelegate <NSObject> - (void)alert:(SPRAlertViewController *)alert didDismissWithButtonClick:(int)buttonIndex; @end
.m文件
#import "SPRAlertViewController.h" #import "SPRAudioHelper.h" @interface SPRAlertViewController () { UIWindow *alertWindow; } @end @implementation SPRAlertViewController +(instancetype)new { SPRAlertViewController *alvc = [super new]; return alvc; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { alertWindow = [self windowWithLevel:UIWindowLevelAlert]; if (!alertWindow) { //rotate the window frame to get the right orientation CGRect tempFrame = [UIScreen mainScreen].bounds; CGRect tempFrame2 = tempFrame; tempFrame2.size.height = tempFrame.size.width; tempFrame2.size.width = tempFrame.size.height; alertWindow = [[UIWindow alloc] initWithFrame:tempFrame2]; alertWindow.windowLevel = UIWindowLevelAlert; [alertWindow makeKeyAndVisible]; NSLog(@"window frame %@", NSStringFromCGRect(alertWindow.frame)); } alertWindow.rootViewController = self; } return self; } - (void)setTitle:(NSString *)title message:(NSString *)message andButtonCount:(int)buttonCount { self.titleLabel.text = title; self.messageLabel.text = message; if (buttonCount == 1) { self.yesButton.hidden = YES; self.noButton.hidden = YES; self.okButton.hidden = NO; } else { self.yesButton.hidden = NO; self.noButton.hidden = NO; self.okButton.hidden = YES; } CGRect frame = self.messageLabel.frame; CGSize size = [self.messageLabel sizeThatFits:CGSizeMake(self.messageLabel.frame.size.width, FLT_MAX)]; frame.size.height = size.height; self.messageLabel.frame = frame; [self.messageLabel setNeedsDisplay]; float buttonTop = frame.size.height + frame.origin.y; self.yesButton.frame = CGRectMake(self.yesButton.frame.origin.x, buttonTop, self.yesButton.frame.size.width, self.yesButton.frame.size.height); self.noButton.frame = CGRectMake(self.noButton.frame.origin.x, buttonTop, self.noButton.frame.size.width, self.noButton.frame.size.height); self.okButton.frame = CGRectMake(self.okButton.frame.origin.x, buttonTop, self.okButton.frame.size.width, self.okButton.frame.size.height); self.alertview.frame = CGRectMake(self.alertview.frame.origin.x, self.alertview.frame.origin.y, self.alertview.frame.size.width, buttonTop+55); NSLog(@"parent frame %@", NSStringFromCGRect(self.parentViewController.view.frame)); NSLog(@"view frame %@", NSStringFromCGRect(self.view.frame)); NSLog(@"alert frame %@", NSStringFromCGRect(self.alertview.frame)); NSLog(@"button frame %@", NSStringFromCGRect(self.yesButton.frame)); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. UIInterpolatingMotionEffect* m1 = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; m1.maximumRelativeValue = @0.0; m1.minimumRelativeValue = @0.0; UIInterpolatingMotionEffect* m2 = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]; m2.maximumRelativeValue = @20.0; m2.minimumRelativeValue = @-20.0; UIMotionEffectGroup* g = [UIMotionEffectGroup new]; g.motionEffects = @[m1,m2]; [self.alertview addMotionEffect:g]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)show { CGRect tempFrame = [UIScreen mainScreen].bounds; CGRect tempFrame2 = tempFrame; tempFrame2.size.height = tempFrame.size.width; tempFrame2.size.width = tempFrame.size.height; self.alertview.center = CGPointMake(CGRectGetMidX(tempFrame2), CGRectGetMidY(tempFrame2)); self.alertview.transform = CGAffineTransformMakeScale(1,1.6); self.alertview.alpha = 0; self.view.alpha = 0; [UIView animateWithDuration:0.1 animations:^{ self.view.alpha = 1; } completion: ^(BOOL finished){ [UIView animateWithDuration:0.25 animations:^{ self.alertview.alpha = 1; self.alertview.transform = CGAffineTransformIdentity; }]; } ]; } - (UIWindow *)windowWithLevel:(UIWindowLevel)windowLevel { NSArray *windows = [[UIApplication sharedApplication] windows]; for (UIWindow *window in windows) { if (window.windowLevel == windowLevel) { return window; } } return nil; } - (IBAction)doDismiss:(id)sender { // [[SPRAudioHelper sharedHelper] playSoundFromTag:lightClick]; NSLog(@"check"); [UIView animateWithDuration:0.25 animations:^{ self.alertview.transform = CGAffineTransformScale(self.alertview.transform, 1,0.5); self.alertview.alpha = 0; }completion:^(BOOL finished) { [UIView animateWithDuration:0.1 animations:^{ self.view.alpha = 0; }completion:^(BOOL finished) { [alertWindow removeFromSuperview]; alertWindow = nil; if (sender == self.yesButton) [self.delegate alert:self didDismissWithButtonClick:1]; else [self.delegate alert:self didDismissWithButtonClick:0]; }]; }]; } @end
这是NSLog的结果:
2014-07-10 10:59:18.603伤害报告计时器[5166:60b]窗口框架{{0,0},{568,320}} 2014-07-10 10:59:18.720伤害报告计时器[5166:60b]父帧{{0,0},{0,0}} 2014-07-10 10:59:18.722伤害报告计时器[5166:60b]查看框架{{0,0},{320,568}} 2014-07-10 10:59:18.724伤害报告计时器[5166:60b]警报框架{{0,51},{568,146.5}} 2014-07-10 10:59:18.726伤害报告计时器[5166:60b]按钮框架{{101,91.5},{150,50}}
答案 0 :(得分:0)
我在做了越来越多的挖掘之后想通了。我真的让整个事情变得更复杂。需要修复2个功能,这里有更新的功能:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
alertWindow = [self windowWithLevel:UIWindowLevelAlert];
if (!alertWindow)
{
alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.windowLevel = UIWindowLevelAlert;
[alertWindow makeKeyAndVisible];
}
alertWindow.rootViewController = self;
}
return self;
}
和
- (void)show
{
CGRect frame = self.view.bounds;
self.alertview.center = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
self.alertview.transform = CGAffineTransformMakeScale(1,1.6);
self.alertview.alpha = 0;
self.view.alpha = 0;
[UIView animateWithDuration:0.1 animations:^{
self.view.alpha = 1;
}
completion:
^(BOOL finished){
[UIView animateWithDuration:0.25 animations:^{
self.alertview.alpha = 1;
self.alertview.transform = CGAffineTransformIdentity;
}];
}
];
}
最初因为节目将视图放在主屏幕边界上,所以它把它放在错误的地方,因为主屏幕边界没有更新轮换。然后我犯了调整窗口大小以适应它的错误。这使按钮不再出现在窗口中。