【问题标题】:How use autoresizing programmatically IOS?如何以编程方式使用自动调整IOS?
【发布时间】:2015-04-23 13:32:37
【问题描述】:

我为 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

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

【问题讨论】:

  • 什么是不工作的?
  • 我通常在情节提要中创建类似的视图并添加具有自动布局的约束。如果我需要在代码中创建一些通用的东西,我会做一些数学运算并将它们放在需要的地方。这都是反复试验。我真的没有掌握编程约束/自动调整大小

标签: ios objective-c iphone autoresize


【解决方案1】:

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

与您的 slideContentView 的所有子视图相同。

【讨论】:

    【解决方案2】:

    以下内容更受反馈:

    1. 这看起来像您的代码是复制/粘贴的,但您的 @property 调用没有指定原子性或 ARC 应如何处理它们,所以这可能有点转述。
    2. 如果您要以编程方式创建视图控制器,它不会调用 initWithNib:
    3. 要将目标/动作分配给按钮,您将使用addTarget:action:forControlEvents:
    4. @synthesize 不需要,除非您要编写自定义设置器
    5. Objc 中的方法总是以小写字母 ([MenuViewController Blur]) 开头
    6. 来自loadView 文档:

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

    7. 如果您将视图控制器的view 属性显式设置为具有[UIScreen mainScreen].bounds 的框架,则无需设置其自动调整大小行为——它已经与屏幕大小匹配。这适用于您正在执行此操作的所有视图。
    8. 不确定此处减去 0 的原因:self.view.frame.size.height - 0 - scrollView.contentOffset.y
    9. 在您的createScrollView 方法中发生了很多事情...如果您的代码中没有 cmets 或显示每个子视图所在位置的某种图像,很难理解。正在设置的视图层次结构似乎过于复杂,最好在自定义子类UIView 或UIScrollView 中完成。我会这样想:如果你一个月不看这段代码,你能轻松说出这个视图会是什么样子吗?
    10. 这可能仅适用于 5,因为您将 UIKit 元素设置到框架中的特定位置,而不是使用自动布局或自动调整大小的蒙版(尽管使用自动布局,这是未来,苹果倾向于停止支持旧技术)

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 2010-09-08
      • 2012-08-13
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多