【问题标题】:Constrain cameraOverlayView to center of screen将 cameraOverlayView 约束到屏幕中心
【发布时间】:2015-07-04 17:01:04
【问题描述】:

我正在创建的 iPad 应用程序中使用 zBAR QR 阅读器,并且我已将自定义十字准线覆盖添加到 ZBarReaderViewController。我已经设法将覆盖层置于中心,但是当设备旋转时,覆盖层显然会偏离中心。

我搜索了有关以编程方式限制视图的方法,但没有任何建议有效。我用UIViewAutoResizingMask 尝试了所有可能的组合,并且我用[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] 方法尝试了很多不同的设置。

这是我的代码:

self.readerqr = [ZBarReaderViewController new];

int readerPointX = ((self.view.bounds.origin.x) + (self.view.bounds.size.width / 2.0));
int readerPointY = ((self.view.bounds.origin.y) + (self.view.bounds.size.height / 2.0) -60);

self.readerqr.readerDelegate = self;
self.readerqr.showsHelpOnFail = NO;

CGRect viewFrame = CGRectMake(readerPointX, readerPointY, 200, 200);

self.crossHair = [[UIImageView alloc] initWithFrame:viewFrame];

self.crossHair.image = [UIImage imageNamed:@"qr.png"];

[self.readerqr setCameraOverlayView:self.crossHair];

constraint = [NSLayoutConstraint constraintWithItem:self.crossHair
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:nil
                                          attribute:NSLayoutAttributeNotAnAttribute
                                         multiplier:1
                                           constant:200];

[self.crossHair addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:self.crossHair
                                          attribute:NSLayoutAttributeWidth
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:nil
                                          attribute:NSLayoutAttributeNotAnAttribute
                                         multiplier:1
                                           constant:200];
[self.crossHair addConstraint: constraint];

什么是正确的设置方法,这样我的叠加层将始终位于屏幕的中心,而不管方向如何变化?任何帮助都会很棒,谢谢。

【问题讨论】:

    标签: ios nslayoutconstraint autoresizingmask zbar-sdk camera-overlay


    【解决方案1】:

    这个问题可能不再相关,但仍然希望这段代码对某人有所帮助。 首先,使用自动布局时,您不需要处理帧。 其次,试试这段代码

    self.readerqr = [ZBarReaderViewController new];
    
    self.readerqr.readerDelegate = self;
    self.readerqr.showsHelpOnFail = NO;
    
    self.crossHair = [[UIImageView alloc] init];
    self.crossHair.translatesAutoresizingMaskIntoConstraints = NO;
    self.crossHair.image = [UIImage imageNamed:@"qr.png"];
    
    [self.readerqr setCameraOverlayView:self.crossHair];
    
    [self.readerqr addConstraint:[NSLayoutConstraint constraintWithItem:self.crossHair
                                                              attribute:NSLayoutAttributeHeight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:nil
                                                              attribute:NSLayoutAttributeNotAnAttribute
                                                             multiplier:1
                                                               constant:200]];
    
    [self.readerqr addConstraint: [NSLayoutConstraint constraintWithItem:self.crossHair
                                                               attribute:NSLayoutAttributeWidth
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:nil
                                                               attribute:NSLayoutAttributeNotAnAttribute
                                                              multiplier:1
                                                                constant:200]];
    
    [self.readerqr addConstraint:[NSLayoutConstraint constraintWithItem:self.crossHair
                                                              attribute:NSLayoutAttributeCenterX
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.readerqr
                                                              attribute:NSLayoutAttributeCenterX
                                                             multiplier:1
                                                               constant:0]];
    [self.readerqr addConstraint:[NSLayoutConstraint constraintWithItem:self.crossHair
                                                              attribute:NSLayoutAttributeCenterY
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.readerqr
                                                              attribute:NSLayoutAttributeCenterY
                                                             multiplier:1
                                                               constant:0]];
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2011-09-27
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多