When personal hotspot gets turned on, the status bar height change from 20 to 40 points, the [[UIScreen mainScreen] applicationFrame] returned CGRect would change from ((0, 20), (screenWidth, screenHeight - 20)) to ((0, 40), (screenWidth, screenHeight - 40)).
When personal hotspot gets turned off, the status bar height change from 40 to 20 points, the [[UIScreen mainScreen] applicationFrame] returned CGRect would change from ((0, 40), (screenWidth, screenHeight - 40)) to ((0, 20), (screenWidth, screenHeight - 20)).
The problem is I initialized my view using the applicationFrame CGRect in the init method, but the Personal Hotspot can be turned on and off at any moment. The view needs to be adjusting its frame in real-time, which I guess it should be something like auto layout or autoresizing, but don't know exactly how to do it?
And, in particular, for iOS 5 devices, auto layout is not available, how can we do it?
Related
I am working on an iOS app in Swift 3.0 and I have integrated card scanner using Card.IO for iOS.
I am successfully able to scan the card, but the problem is that width and height of the camera view does not take frames as per the requirements.
It only takes width and height in ratio of 3:4.
I want, the camera should take half of the screen height and full screen width, But is is not taking. When I pass the frame as
cardView = CardIOView(frame: CGRect(x: 0, y: 100, width: screen.width, height: screen.height / 2))
it does not take full screen width.
Is it a bug on SDK side, I have tried everything but no success.
If anyone can help.
Thanks in advance!
My suggestion is: you incorrectly determine the screen size: Swift: Determine iOS Screen size
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
I am facing one strange problem, using the below code I am making the UIScrollView to full screen.
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
CGRect scrollFrame = CGRectMake(0, 0, screenWidth, screenHeight);
self.imageHostScrollView.frame = scrollFrame;
NSLog(#"Scroll Height: %f, Width: %f",screenHeight,screenWidth);
The problem I am facing when the iPad is in the Portrait mode, the height will be big and width will be small, instead I am getting height smaller and width bigger (same happens in Landscape mode also).
Portrait mode the value I am getting is
Scroll Height: 768.000000, Width: 1024.000000
In Landscape mode the value I am getting is
Scroll Height: 1024.000000, Width 768.000000
Can anyone help me
The problem is that the way you're setting the self.imageHostScrollView.frame is silly. You are effectively hard-coding assumptions about the screen into the frame of a view — two things that are completely unrelated to one another.
Instead, use auto layout to pin the edges of the scroll view to the edges of the window. That way, whatever the window may do from now on — making no assumptions about what that may be — the scroll view will continue to fill it exactly.
Our current version of our iphone application in the app store simply stretches the layout to fill the iphone 6 and 6+. After the xcode 7 update, the app just runs as it would on an iphone 4 with black bars at the top. I understand that adding a launch storyboard resolves this. The problem then is that our layout is out of whack on the 6 and 6+.
Obviously fixing the layout to adapt to these screen sizes is what needs to be done, but we are in a pinch and just need to get a behind the scenes function update pushed out to our users ASAP. Is there anyway to go back to the old behavior where the UIView would just stretch to fill the screens?
This won't be an immediate fix, but it's a quick patch that might be worth trying.
If your view is always in portrait, this should work for you. If it requires rotating or a UISplitViewController on iPhone 6(s)+, then it might not.
Scale each subview in your view controller for the new screen size. Declare a couple macros for the device screen size:
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
Then add a method to your view controllers that goes through all subviews to resize each view proportionately based on an iPhone 4 screen size.
- (void)resizeSubviews:(UIView *)view {
CGRect frame = view.frame;
frame.origin.x = frame.origin.x / 320.0f * kScreenWidth;
frame.origin.y = frame.origin.y / 480.0f * kScreenHeight;
frame.size.width = frame.size.width / 320.0f * kScreenWidth;
frame.size.height = frame.size.height / 480.0f * kScreenHeight;
view.frame = frame;
for (UIView *subview in view.subviews) {
[self resizeSubviews:subview];
}
}
Then call that method after adding your subviews with:
[self resizeSubviews:self.view];
There are a lot of ways this could not work, but it covered me during the switch from iPhone 4s to iPhone 5.
I'm new to iOS development and for my assignment, I'm tasked changing updating the ViewController programmatically when the device's orientation changes. I've found a snippet of an answer here, but it doesn't get the job done.
I tried adding this to the viewWillLayoutSubviews of my View Controller, but all I get is an unused variable warning.
CGRect rotatedFrame = [self.view convertRect:self.view.frame fromView:self.view.superview];
viewWillLayoutSubviews and rotation
As a "hint", I've been told it's simple to implement in viewWillLayoutSubviews. Going through and changing all the CGRects in my VC doesn't sound like a couple of lines of code. There's got to be a simpler, more efficient way to do this, but I've only found snippets of solutions digging around on this site. Thanks for reading.
The line of code you are using is assigning a CGRect to the rotatedFrame variable, it's not updating anything on your view controller.
There's many ways to approach this but it depends on what is contained in your view and how it's been configured. Things like Auto Layout for example could let you configure almost everything in Interface Builder and let you avoid doing most things in code.
You've been tasked to do this programatically and since we know that viewWillLayoutSubviews is called every time the device is rotated that's a good place to start. Here's a lazy way I've gone about rotating a video to fit a new orientation using a transformation:
//Vertical
CGSize size = self.view.frame.size;
someView.transform = CGAffineTransformMakeRotation((M_PI * (0) / 180.0))
someView.frame = CGRectMake(0, 0, MIN(size.width, size.height), MAX(size.width, size.height));
//Horizontal
CGSize size = [UIScreen mainScreen].bounds.size;
int directionModifier = ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft) ? -1 : 1;
someView.bounds = CGRectMake(0, 0, MAX(size.width, size.height), MIN(size.width, size.height));
someView.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0) *directionModifier);
someView.transform = CGAffineTransformTranslate(someView.transform,0,0);
How many subviews are in your view? Are they grouped? If you're using auto-resizing masks you might get away with only adjusting the frames of one or two views. If your root view has a number of subviews you can loop through views that need similar adjustments to avoid having to write excess code. It really depends on how everything has been set up.
I figured out how to determine the viewWidth and viewHeight and set those as CGFloats. I then added an if-else statement which figures out if the display is in portrait or landscape and sets the problematic calculateButton accordingly.
Apologies for lengthy code, but I've found in searching this site I find "snippets" of answers, but being new to iOS it's difficult to figure out what goes where. Hopefully, this helps someone later. (and hopefully it's correct)
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGFloat viewWidth = self.view.bounds.size.width;
CGFloat viewHeight = self.view.bounds.size.height;
CGFloat padding = 20;
CGFloat itemWidth = viewWidth - padding - padding;
CGFloat itemHeight = 44;
// Bunch of setup code for layout items
// HOMEWORK: created if-else statement to deal w/ portrait vs. landscape placement of calculateButton.
if (viewWidth > viewHeight) {
// portrait
CGFloat bottomOfLabel = viewHeight;
self.calculateButton.frame = CGRectMake(padding, bottomOfLabel - itemHeight, itemWidth, itemHeight);
} else {
// landscape
CGFloat bottomOfLabel = CGRectGetMaxY(self.resultLabel.frame);
self.calculateButton.frame = CGRectMake(padding, bottomOfLabel + padding, itemWidth, itemHeight);
}
}
I'm trying to create multiple views for an iPad app. First I created a menu view and then a sub-menu view with coordinates according to menu. So it looks like this:
What I did:
self.view = [[UIView alloc] initWithFrame:CGRectMake(self.parentViewController.view.frame.size.width, 0, 150, screenHeight)];
But now on sub-menu I'm trying to create the content view, which is a UINavigationController. Trying to do the same thing, I get this result:
What I'm doing (this time creating the frame on sub-menu view controller):
CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width,
0,
[[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width,
[[UIScreen mainScreen] bounds].size.height);
It's pretty self-explanatory but, I just get the sub-menu origin and add its width so I can get the right edge coordinate.
After a lot of attempts I managed to get it working, because I noticed that the CGRectMake is using the center of the UINavigationController view to arrange its position. So the following code:
CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width + 259,
0,
[[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width,
[[UIScreen mainScreen] bounds].size.height);
Yields the right position.
What's going on? I thought CGRectMake origin would always be top-left, but on this particularly view is actually top-middle (or middle-middle, not sure.) What am I doing wrong?
EDIT1:
Here's the console output for the frame with right position:
Notice how the nav bar is now positioned right:
But the x-coord is not 250 (as it should be, because menu.width + sub-menu.width = 250.)
EDIT2:
I eventually gave up. The problem was with the automatically generated UINavigationBar, which is created by the UINavigationViewController. I can't seem to figure out how to configure it. I'm gonna leave the question open in case someone knows the answer.
Center of a CGRect is a CGPoint created from the origin.x + ( size.width / 2 ) and origin.y + ( size.height / 2 ).
UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
This will make the button in the center no matter what the device is
CGRectMake just creates an stucture of (x,y, witdh, height).
It your part to set the correct values.