My Button Is Centered for iPhone 6 and 6 Plus, But Not for iPhone 5

How to specify different sizes for iPhone 6+, 6 and 5?

Why do you need to pick 30, 35 and 45px ? Is there a reason for that pixelish placement ?


You can use more flexible constraints by using their multiply value instead of the constant.

The constraints adjusts themselves depending on the superview dimensions.

iPhone5 - iPhone 6 - iPhone 6+ - iPad - iPhone4 landscape
Constraints demo

Note how my center X alignment constraints are configured:

Sample Image


Sample Image

You can see that the values behaves kind of strangely, sometimes multiplier is > 1, sometimes it's between 1 and 2, or < 1 depending on the order of First Item and Second Item. You still can swap their order by clicking on them and selecting Reverse First And Second Item.

iOS - Need to resize a button and image for different iPhones

Thank you Zhang.
I went ahead and created a new asset catalog with three images, one for each size of iPhone, and then simply dragged the images into that folder in the XCode editor window.

The contents.json asset catalog file looks like this:

Contents.json :

{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "MainImage.png"
},
{
"idiom" : "universal",
"scale" : "2x",
"filename" : "MainImage@2x.png"
},
{
"idiom" : "universal",
"scale" : "3x",
"filename" : "MainImage@3x.png"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

With the exception that I am using Swift which makes this less code and therefore simpler, your answer is spot on. In Swift, there is no more need for two files for the ViewController, only one. I also was able to assign my sound to a single button within the ViewController, removing the need for any ImageView container for my background image:

ViewController :

import UIKit
import AVFoundation

class ViewController: UIViewController {

var myPlayer = AVAudioPlayer()

var yourSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("RemSound_01", ofType: "wav")!)
func initYourSound() {
myPlayer = AVAudioPlayer(contentsOfURL: yourSound, error: nil)
myPlayer.prepareToPlay()
}
override func viewDidLoad() {
super.viewDidLoad()
initYourSound()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func playMySound(sender: AnyObject) {
myPlayer.play()
}
}

I hope this helps anyone starting out coding with Swift in XCode.

Proportional layout for iPhones (from 5 to 6 Plus) with Size Classes

If you want to make such poor use of bigger screen, you could simply not include launch file or launch images for 4.7" and 5.5" devices and they will just use a scaled-up 4" layout.

But please don't do any of that. The screens might be the same aspect ratio, but they are not the same size and treating them that way makes for a bad user experience.

UIModalPresentationPopover for iPhone 6 Plus in landscape doesn't display popover

Implement the new adaptivePresentationStyleForPresentationController:traitCollection: method of UIAdaptivePresentationControllerDelegate:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
// This method is called in iOS 8.3 or later regardless of trait collection, in which case use the original presentation style (UIModalPresentationNone signals no adaptation)
return UIModalPresentationNone;
}

UIModalPresentationNone tells the presentation controller to use the original presentation style which in your case will display a popover.

Alignment of buttons in view

I might be missing something here. But is it all that you need, to have multiple buttons centered vertically in the container and also horizontally (with no overlap and good symmetry). If so, you can do the below.

The constraints are -

  1. Button A - Center vertically in container

  2. Button B:Center Y = Button A:Center Y; Button C:Center Y = Button B:Center Y

  3. Button A: Leading space to superview = some constant (say 30)

  4. Button C: Trailing space to superview = same constant as above

  5. Horizontal spacing (Button A - Button B) = Horizontal spacing (Button B - Button C) = some constant (say 10)

  6. Width (Button A) = Width (Button B) = Width (Button C)

Sample Image

Button At Center of iPhone 3.5 and iPhone 4 inch using AutoLayout?

Question 1:
This should be pretty easy in IB, using the Alignment Constraints dialog. Click the button, and in the Alignment Constraints, check Horizontal Center in Container and Vertical Center in Container. In the below collage, you can see how this centers the button on both 3.5 and 4 inch displays.
(Don't forget to click the button "Add 2 constraints" :-) )

Question 2:
Not sure what you are after... Formula?

Interface Builder

Why isn't preferredContentSize used by iPhone 6 Plus Landscape?

What you're seeing is not a popover. It's a normal presented view. By default, a popover appears as a popover on iPad, but as a presented view on iPhone — including the iPhone 6 plus. On other iPhones, this presented view is fullscreen - it covers everything. But the iPhone 6 is so wide that they don't do that, so it appears in the middle of the screen at a standard width (the width of a smaller iPhone).

Thus, the preferred content size has no effect. This isn't a popover. Presented view controller views are given a standard size, and this one is no exception.

However, you can have the popover appear as a popover on iPhone. To do so:

  • Set a delegate for the popover view controller's popoverPresentationController before presenting it.

  • In the delegate, implement adaptivePresentationStyleForPresentationController: and return .None.

However, this is apparently not working on iPhone 6 Plus in landscape mode; the popover is not "adapting". I would describe this as a bug!

EDIT In iOS 9, the problem is solved! Implement the new delegate method adaptivePresentationStyleForPresentationController:traitCollection: to return .None and you'll get a popover under all circumstances, including the iPhone 6 Plus in landscape. Here's a complete working example where the popover is created and summoned in code in response to a button tap:

@IBAction func doButton(sender: AnyObject) {
let vc = MyViewController()
vc.preferredContentSize = CGSizeMake(400,500)
vc.modalPresentationStyle = .Popover
if let pres = vc.presentationController {
pres.delegate = self
}
self.presentViewController(vc, animated: true, completion: nil)
if let pop = vc.popoverPresentationController {
pop.sourceView = (sender as! UIView)
pop.sourceRect = (sender as! UIView).bounds
}
}
func adaptivePresentationStyleForPresentationController(
controller: UIPresentationController,
traitCollection: UITraitCollection)
-> UIModalPresentationStyle {
return .None
}

Sample Image



Related Topics



Leave a reply



Submit