2016-04-25 2 views
0

私のUIViewは、iPhoneの画面サイズによって異なる位置に表示されるのはなぜですか?私はそれがautoresizingMaskのためだと思いますが、autoresizingMaskを適用した後、どのようにビューのフレームを得ることができますか、いくつかの普遍的なロジックを作成するには?またはこれを処理するベストプラクティスは何ですか?なぜ私のUIViewは異なるiPhone上で異なる位置にありますか?

private func setup() { 

     positionView = CircularProjectPositionControl(frame: self.bounds) 
     positionView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
     positionView.outerCircleWidth = outerCircleWidth 

     assetsView = CircularProjectAssetsControl(frame: self.bounds) 
     assetsView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
     assetsView.backgroundColor = UIColor.clearColor() 
     assetsView.imageWidth = outerCircleWidth 

     let size = CGSize(width:40, height:40) 
     let width = floor(CGRectGetMidX(self.bounds) - size.width/2.0) 
     let height = floor(CGRectGetMidY(self.bounds) - size.width/2.0) 
     collaboratorView = AvatarView(frame: CGRect(x: width, y: height, width: size.width, height: size.height)) 
     collaboratorView.autoresizingMask = [.FlexibleBottomMargin, .FlexibleLeftMargin, .FlexibleRightMargin] 

     self.addSubview(assetsView) 
     self.addSubview(positionView) 
     self.addSubview(collaboratorView) 
    } 

iPhone 6S: enter image description here

iPhone 5:あなたが唯一のself.viewとの相対位置を設定するよう enter image description here

+0

は、なぜあなたはプログラムでこれをやっているの?ストーリーボードの自動レイアウト制約を使用する方がはるかに簡単です –

+0

@ matt.writes.codeここではストーリーボードを使用できません –

答えて

0

ルックスここ

は、コードがあります。 3つの円にお互いに同じ相対位置が残るようにするには、それらの間に制約を設定する必要があります。

+0

自動レイアウト制約または自動サイズ変更マスクを追加する必要がありますか? –

1

サブビューのそれぞれを中央に.CenterX & .CenterY contraints追加してみてください:

NSLayoutConstraint(item: view1, attribute: .CenterX, relatedBy: .Equal, toItem: view2, attribute: .CenterX, multiplier: 1, constant: 0).active = true 
NSLayoutConstraint(item: view1, attribute: .CenterY, relatedBy: .Equal, toItem: view2, attribute: .CenterY, multiplier: 1, constant: 0).active = true 
関連する問題