ios - 如何以编程方式使用 autoresizing IOS?

标签 ios objective-c iphone autoresize

我为带有一些 subview 的 Iphone 5 (8.1) 制作了一个没有 Storyboard的代码。它适用于 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

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

最佳答案

以下内容更多地属于反馈:

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

    Your custom implementation of this method should not call super.

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

关于ios - 如何以编程方式使用 autoresizing IOS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653245/

相关文章:

ios - 为什么辞去 First Responder 不隐藏键盘?

ios - FFMPEG 将 .avi 转换为可播放的 iOS 电影 mp4

ios - 使用自定义 URL 方案的 UIWebView 加载失败

ios - 使用我的 ViewController 作为 TabBarController

ios - 是否可以在 iOS 上下载并运行代码

ios - "fatal error: unavailable function can' 不被调用”

ios - 如何根据 TableView 单元格的详细文本标签将 segue 设置为与 TableView Controller 不同的 View Controller ?

ios - keyboardWillShow 如何在 Swift iOS 中仅应用于 1 个 textView

ios - 删除 iOS 版本而不上传构建

ios - SVProgressHUD iOS8 用动画替换圆形