如何使用IOS编程自动化?

时间:2015-02-22 00:51:44

标签: ios objective-c iphone autoresize

我为Iphone 5(8.1)创建了一个没有故事板的代码,带有一些子视图。它适用于Iphone 5设备,但对其他设备不起作用......我试图插入自动调整功能,但我不知道为什么它仍然无法正常工作。

这是我的代码:

#import "MenuViewController.h"
#import "RevealViewController.h"

@interface MenuViewController ()
@property UIView *blurMask;
@property UIImageView *blurredBgImage;
@end

@implementation MenuViewController
@synthesize blurMask, blurredBgImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{

    _barButton.target = self.revealViewController;
    _barButton.action = @selector(revealToggle:);

    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    [super viewDidLoad];
    [self Blur:blurredBgImage];
}

- (void)loadView{
    [super loadView];
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view.backgroundColor = [UIColor whiteColor];
    [[self view] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [[self view] setAutoresizesSubviews:YES];

    // content view
    [self.view addSubview:[self createContentView]];

    // slide view
    [self.view addSubview:[self createScrollView]];

    blurMask = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    blurMask.backgroundColor = [UIColor whiteColor];
    blurredBgImage.layer.mask = blurMask.layer;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    blurMask.frame = CGRectMake(blurMask.frame.origin.x,
                            self.view.frame.size.height - 0 - scrollView.contentOffset.y,
                            blurMask.frame.size.width,
                            blurMask.frame.size.height + scrollView.contentOffset.y);
}

- (UIView *)createContentView
{
    UIView *contentView = [[UIView alloc] initWithFrame:self.view.frame];
    [contentView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [contentView setAutoresizesSubviews:YES];

    UIImageView *contentImage = [[UIImageView alloc] initWithFrame:contentView.frame];
    contentImage.image = [UIImage imageNamed:@"back-ground.png"];
    [contentView addSubview:contentImage];
    return contentView;
}

- (UIView *)createScrollView{
    UIView *containerView = [[UIView alloc] initWithFrame:self.view.frame];
    [containerView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [containerView setAutoresizesSubviews:YES];

    blurredBgImage = [[UIImageView  alloc] initWithFrame:CGRectMake(0, -57, self.view.frame.size.width, 568)];
    [blurredBgImage setContentMode:UIViewContentModeScaleToFill];
    [containerView addSubview:blurredBgImage];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [containerView addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width,  self.view.frame.size.height*2 - 110);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;

    UIView *slideContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 455, self.view.frame.size.width, 508)];

    slideContentView.backgroundColor = [UIColor clearColor];
    [scrollView addSubview:slideContentView];

    UILabel *slideUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 6, self.view.frame.size.width, 50)];
    slideUpLabel.text = @"Informations";
    [slideUpLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:18]];
    [slideUpLabel setTextAlignment:NSTextAlignmentCenter];
    [slideUpLabel setContentMode:UIViewContentModeScaleToFill];
    slideUpLabel.textColor = [UIColor colorWithWhite:1 alpha:0.5];
    [slideContentView addSubview:slideUpLabel];

    UIImageView *slideUpImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 12, 4, 24, 22.5)];
    slideUpImage.image = [UIImage imageNamed:@"up-arrow.png"];
    [slideContentView addSubview:slideUpImage];

    UITextView *detailsText = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, 275, 350)];
    detailsText.backgroundColor = [UIColor clearColor];
    detailsText.text = @"Details text… with information ";
    [detailsText setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:16]];
    [detailsText setTextAlignment:NSTextAlignmentCenter];
    detailsText.textColor = [UIColor colorWithWhite:1 alpha:0.6];

    [slideContentView addSubview:detailsText];

    UIImageView *Facebook = [[UIImageView alloc] initWithFrame:CGRectMake(105, 260, 42, 42)];
    Facebook.image = [UIImage imageNamed:@"facebook.png"];

    [slideContentView addSubview:Facebook];

    UIImageView *Twitter = [[UIImageView alloc] initWithFrame:CGRectMake(168, 260, 42, 42)];
    Twitter.image = [UIImage imageNamed:@"twitter.png"];

    [slideContentView addSubview:Twitter];

    UITextView *rsText = [[UITextView alloc] initWithFrame:CGRectMake(20, 300, 275, 350)];
    rsText.backgroundColor = [UIColor clearColor];
    rsText.text = @"Text Facebook text Twitter. mail ";
    [rsText setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:16]];
    [rsText setTextAlignment:NSTextAlignmentCenter];
    rsText.textColor = [UIColor colorWithWhite:1 alpha:0.6];

    [slideContentView addSubview:rsText];

    UITextView *endText = [[UITextView alloc] initWithFrame:CGRectMake(25, 450, 270, 350)];
    endText.backgroundColor = [UIColor clearColor];
    endText.text = @"Thank you ";
    [endText setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12]];
    [endText setTextAlignment:NSTextAlignmentCenter];
    endText.textColor = [UIColor colorWithWhite:1 alpha:0.6];

    [slideContentView addSubview:endText];

    return containerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)Blur:(UIView *)view {
    UIView *blurView=nil;
    if ([UIBlurEffect class]) {
        UIBlurEffect *blurEffect= [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        blurView.frame = view.frame;
    }else {
        blurView = [[UIToolbar alloc] initWithFrame:view.bounds];

    }
    [blurView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [view addSubview:blurView];
    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[blurView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(blurView)]];
    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[blurView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(blurView)]];

}

@end

有什么想法吗?反馈? 谢谢。

2 个答案:

答案 0 :(得分:1)

以下内容更多地反馈:

  1. 看起来您的代码是复制/粘贴的,但是您的@property调用没有指定原子性或ARC应该如何处理它们,所以也许这有点被解释了。
  2. 如果您以编程方式创建视图控制器,则不会调用initWithNib:
  3. 要将目标/操作分配给按钮,您将使用addTarget:action:forControlEvents:
  4. 除非您要编写自定义setter ,否则不需要
  5. @synthesize Objc中的
  6. 方法始终以小写字母([MenuViewController Blur]
  7. 开头
  8. 来自loadView文档:

      

    此方法的自定义实现不应调用super。

  9.   
  10. 如果您明确地将视图控制器view属性设置为具有[UIScreen mainScreen].bounds的帧,则不需要设置其自动调整行为 - 它已经与大小相匹配屏幕。这适用于您为此所做的所有观点。
  11.   
  12. 不确定这里减去0的原因:self.view.frame.size.height - 0 - scrollView.contentOffset.y
  13.   
  14. 您的createScrollView方法中有相当多的内容......如果您的代码中没有评论,或者某些图片显示每个子视图的位置,则很难关注。设置heirarchy的视图看起来过于复杂,最好在自定义子类UIViewUIScrollView中完成。我会这样想:如果你没有看一个月的代码,你能轻易告诉这个视图会是什么样子吗?
  15.   
  16. 当您将UIKit元素设置为框架中的特定位置时,这可能仅适用于5,而不是使用自动布局或自动调整遮罩(尽管使用自动布局,但这是未来,苹果有倾向于停止支持旧技术)
  17.   

答案 1 :(得分:0)

如果没有更多上下文,我不确定您的代码的哪些部分可能是故意的。但是,看起来您正在向contentView和scrollView添加自动调整遮罩,但不会添加这两者的任何子视图。例如,在createContentView方法中,如果希望在contentView调整大小时调整UIImageView的大小,则还需要向UIImageView添加自动调整约束。即imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

与slideContentView的所有子视图相同。