I'm using zBAR QR Reader in an iPad app im creating and I've added a custom crosshair overlay to the ZBarReaderViewController
. I've managed to get the overlay into the center, however when the device rotates, the overlay obviously goes off center.
I've had a search around in regards to programmatically constraining the view but none of the suggestions have worked. I've tried every possible combination with UIViewAutoResizingMask
and I've tried heaps of different setups with the [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]
method.
Here's my code:
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];
What is the proper way to set this up so my overlay will always be in the center of the screen regardless of orientation change? Any help would be great, thanks.
This question might not be relevant anymore, but still hope this code helps someone. First of all, when using autolayout you do not need to deal with frames. Secondly, try this code
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]];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.