ios8 [view setFrame:rect] not working - objective-c

I am not able to reposition my uiview in ios8 programatically.The following code seems to be working perfectly fine in ios7.1 but is not working in ios8.
This code is inside layoutSubview function of the view.
for (UIView *child in self.subviews)
{
if((self.removeOnHide == YES && child.hidden == NO) || self.removeOnHide == NO) {
top += self.spacing;
switch (self.hAlignment)
{
case UIControlContentHorizontalAlignmentLeft:
left = self.leftMargin;
break;
case UIControlContentHorizontalAlignmentRight:
left = self.frame.size.width - self.rightMargin
- child.frame.size.width;
break;
default: // center
left = baseline - child.frame.size.width / 2;
break;
}
CGRect rect = child.frame;
child.frame = CGRectMake(left, top, child.frame.size.width,
child.frame.size.height);
rect = child.frame;
top += child.frame.size.height;
}
}
I am using code from this git repos https://github.com/tazihosniomar/LayoutManager. The code use to work perfectly fine in ios7.1 but is not working in ios8.0

Related

Camera is not rotating properly in iOS8 beta for old projects

I'm working on an iOS Application which is created using older X-Code version, so it has window in MainWindow.xib.
Recently when I run this project in iOS8 Beta version, I got a problem: window is not rotating in landscape orientation. So I have added another window in MainWindow.xib. I made if condition to check OS Versions, for older iOS version I'm using previously created window but for iOS8 I'm using newly added window for landscape orientation. I have solved the problem. The entire application is working properly except the camera.
Below are screen shots.
In LandscapeMode(It's working properly):
In PortraitMode(Once camera is loaded, i have rotated it,Here other controls are rotating properly, but camera overly is not rotating properly):
Once Camera is loaded in landscape mode I have rotated device, the controls are rotating properly but the camera screen is not rotating, it is still in portrait mode.
This may be due to window in Appdelegate.
Any solution for this?
Thanks in advance.
I am having the same issue.
I was tryed to use the cameraViewTransform.
Below
#import "MyUIImagePickerController.h"
#implementation MyUIImagePickerController
bool rotateFlg = false;
UIInterfaceOrientation startupOrientation;
-(id)init{
startupOrientation = [[UIApplication sharedApplication] statusBarOrientation];
return [super init];
}
-(NSUInteger)supportedInterfaceOrientations {
// iOS 8
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
float lotate[4];
if (startupOrientation == UIInterfaceOrientationLandscapeRight || startupOrientation == UIInterfaceOrientationLandscapeLeft) {
lotate[0] = 90.0f;
lotate[1] = -90.0f;
lotate[2] = 180.0f;
lotate[3] = 0.0f;
} else {
lotate[0] = 0.0f;
lotate[1] = 180.0f;
lotate[2] = 90.0f;
lotate[3] = -90.0f;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation ==UIInterfaceOrientationPortrait ){
CGAffineTransform __rotation = CGAffineTransformMakeRotation(lotate[0] * M_PI / 180.0f);
self.cameraViewTransform = __rotation;
rotateFlg = true;
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown){
CGAffineTransform __rotation = CGAffineTransformMakeRotation(lotate[1] * M_PI / 180.0f);
self.cameraViewTransform = __rotation;
rotateFlg = true;
} else if(orientation == UIInterfaceOrientationLandscapeLeft){
if (rotateFlg) {
CGAffineTransform __rotation = CGAffineTransformMakeRotation(lotate[2] * M_PI / 180.0f);
self.cameraViewTransform = __rotation;
rotateFlg = false;
}
}else if(orientation == UIInterfaceOrientationLandscapeRight){
if (rotateFlg) {
CGAffineTransform __rotation = CGAffineTransformMakeRotation(lotate[3] * M_PI / 180.0f);
self.cameraViewTransform = __rotation;
rotateFlg = false;
}
}
}
return UIInterfaceOrientationMaskAll;
}
#end
It seems to work
But the source is not beautiful ...
Have posted what I believe is a solution, over at; https://stackoverflow.com/a/26934067/782533
Looks like the same issue? Not sure if I'm supposed to link or copy/paste, so here it is anyway;
I had the same issue and after getting back to basics and creating a new project which actually worked.. I tracked it down to;
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
// Had forgot to call the following..
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
Hope it helps.
According to this thread on Apple Dev forums rotation in iOS8/XCode6 has some problems when using XIBs but not when using Storyboards.

NSSplitView Fixed Splitter on Window Resize?

I'm having a bit of difficulty getting a NSSplitView to behave itself.
What I have at the moment is:
NSWindow
NSView
NSSplitView
navView <NSView>
contentView <NSView>
The problem I'm having is with the splitter shifting position when I resize the window.
In the split view delegate I've already got:
-(CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex{
return 200;
}
-(CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex{
return 200;
}
Despite this the splitter still moved when I resize the window.
if I click ont he splitter, it snaps back to 200px as it should. How can I stop this from moving?
I've tried Autolayout, which is a bit of a nightmare to use, so I've literally disabled it and manually tried to do it with no joy..
Any ideas?
I wrote the above code for Swift and extended it with the possibility to define, whether the left or the right view has to be preferred:
var preferringLeftSideOfSplitView = true
func splitView(splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {
var dividerThickness = splitView.dividerThickness
var leftRect = splitView.subviews[0].frame
var rightRect = splitView.subviews[1].frame
// Resizing and placing the left view
splitView.subviews[0].setFrameOrigin(NSMakePoint(0, 0))
if self.preferringLeftSideOfSplitView == true {
splitView.subviews[0].setFrameSize(NSMakeSize(leftRect.width, splitView.frame.size.height))
} else {
splitView.subviews[0].setFrameSize(NSMakeSize(splitView.frame.size.width - rightRect.width - dividerThickness, splitView.frame.size.height))
}
// Resizing and placing the right view
if self.preferringLeftSideOfSplitView == true {
splitView.subviews[1].setFrameOrigin(NSMakePoint(leftRect.size.width + dividerThickness, 0))
splitView.subviews[1].setFrameSize(NSMakeSize(splitView.frame.size.width - leftRect.size.width - dividerThickness, splitView.frame.size.height))
} else {
splitView.subviews[1].setFrameOrigin(NSMakePoint(splitView.frame.size.width - rightRect.width, 0))
splitView.subviews[1].setFrameSize(NSMakeSize(rightRect.size.width, splitView.frame.size.height))
}
}
I've worked it out...
-(void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
CGFloat dividerThickness = [sender dividerThickness];
NSRect leftRect = [[[sender subviews] objectAtIndex:0] frame];
NSRect rightRect = [[[sender subviews] objectAtIndex:1] frame];
NSRect newFrame = [sender frame];
leftRect.size.height = newFrame.size.height;
leftRect.origin = NSMakePoint(0, 0);
rightRect.size.width = newFrame.size.width - leftRect.size.width
- dividerThickness;
rightRect.size.height = newFrame.size.height;
rightRect.origin.x = leftRect.size.width + dividerThickness;
[[[sender subviews] objectAtIndex:0] setFrame:leftRect];
[[[sender subviews] objectAtIndex:1] setFrame:rightRect];
}
- (BOOL)splitView:(NSSplitView *)aSplitView shouldAdjustSizeOfSubview:(NSView *)subview
{
return subview == rightView;
}
add this to your delegate.
if your NSSplitView would contain lefView and rightView, this keeps leftView at a fixed width, and rightView will resize, when resizing the main window.

masterView does not always show/hide properly

I have a splitViewController. This is in Detail VC
-(void)viewDidLoad
{
self.masterIsVisible = YES;
//a botton in navigation bar to hide or show the master view.
[button addTarget:self action:#selector(showOrHideMasterView)
forControlEventsTouchUpInside]
//gesture control to swipe right or left to slide master view in and out.
[swiperight addTarget:self action:#selector(showMasterView)];
[swipLeft addTarget:self action:#selector(hideMasterView)];
}
-(void)showOrHideMasterView
{
if (self.masterIsVisible)
[self hidemasterView]; self.masterIsVisible = NO;
else
[self showMasterView]; self.masterIsVisible = YES;
}
-(void)hideMasterView
{
//hides master view by substracting masterview's width from its origin.x
}
-(void)showMasterView
{
//shows master View by adding masterview's width to its origin.x
}
- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController: (UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
return NO;
}
Everything almost works as intended.
Problem: In one orientation && master is NOT visible.. then device changes orientation.. the master View instead of sliding off the screen pushed the detail view the other way. I know thats because the flag now is set as masterIsVisible = NO instead of YES. What can I do to change the flag to YES on device rotation. looks trivial but cant seem to figure out.
I tried registering for devicechnagenotification in UIDevice but that did not work. The BOOL is YES in any Orientation. The apple example uses this but looks like thats not the right approach here.
Ok, I finally figured out to set the flag correctly for orientation change. I added the following method
-(void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
self.masterIsVisible = NO;
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
self.masterIsVisible = YES;
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
self.masterIsVisible = YES;
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
self.masterIsVisible = NO;
}

Dragging a UITableView

I am using a UIPanGestureRecognizer to allow my UITableView to be dragged around. I have currently set it so that the UITableView cannot be dragged past 0 or half its width. But now, its frame is set to 0 when I try and drag the UITableView back to 0 from an origin of greater than 0. How can I prevent this and allow dragging of the UITableView back to 0? I have tried the following, but I can't quite find out why the outlined code is causing this.
- (void) handlePan:(UIPanGestureRecognizer *) pan {
CGPoint point = [pan translationInView:_tableView];
CGRect frame = [_tableView frame];
if (point.x <= _tableView.frame.size.width / 2) {
frame.origin.x = point.x;
}
NSLog(#"%f : %f", frame.origin.x, _tableView.frame.origin.x);
//outline begin!
if (frame.origin.x < 0 && _tableView.frame.origin.x >= 0) {
frame.origin.x = 0;
}
//outline end!
isFilterViewShowing = frame.origin.x > 0;
[_tableView setFrame:frame];
}
This is not the prettiest code, but that is working in the simulator.
For this code to work you need to add an instance variable.
This code may not behave exactly as you want it, because it's keeping track of "negative" x position, so you get some "threshold" effect that you may not want depending on you design choice.
- (void) handlePan:(UIPanGestureRecognizer *) pan {
if (pan.state == UIGestureRecognizerStateBegan)
{
// cache the starting point of your tableView in an instance variable
xStarter = _tableView.frame.origin.x;
}
// What is the translation
CGPoint translation = [pan translationInView:self.tableView];
// Where does it get us
CGFloat newX = xStarter + translation.x;
CGFloat xLimit = self.tableView.superview.bounds.size.width / 2;
if (newX >= 0.0f && newX <= xLimit)
{
// newX is good, don't touch it
}
else if (newX < 0)
{
newX = 0;
}
else if (newX > xLimit)
{
newX = xLimit;
}
CGRect frame = self.tableView.frame;
frame.origin.x = newX;
[_tableView setFrame:frame];
if (pan.state == UIGestureRecognizerStateEnded)
{
// reset your starter cache
xStarter = 0;
}
}
Have you notice how [pan translationInView:aView]; is returning the offset of the pan gesture and not the position of the finger on the screen.
That is why your code don't work as you expect.

UIScrollView Custom View rotation problem

I have a ScrollView with a Custom View. Now i have the problem with the rotation, the view has after the rotation not the correct frame pos / size.
How can i call the CustomView after rotation for a reposition and resize the frame and content?!
- (void)setupPage
{
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
if (nimages == list.count) {
break;
}
CustomStepView *stepView = [[CustomStepView alloc] initWithFrame:CGRectZero];
stepView.tag = nimages;
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
stepView.frame = CGRectMake(cx, 0.0 , 768.0f, 926.0f);
cx += 768.0;
}
else
{
stepView.frame = CGRectMake(cx, 0.0 , 1024.0f, 670.0f);
cx += 1024.0;
}
[scrollView addSubview:stepView];
[stepView release];
}
self.pageControl.numberOfPages = nimages;
}
From what it looks like, You have a lot of subviews(Custom Views) in your ScrollView.
First of all never try to detect the orientation using UIDevice , it is not recommended by apple.
For handling rotations you have been provided with the function : "shouldAutorotateToInterfaceOrientation"
Inside this method, You can simple run a loop for detecting the subviews present inside your scrollview and then set the frame and size of each subview.
This might look something like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait)
{
for(UIView *asubview in scrollView.subviews)
{
asubview.frame = "NEW FRAME";
}
}
else
{
asubview.frame = "NEW FRAME";
}
}
Hope it helps you..although i know this has been asked long back :-)
Cheers