swiftでボタンを押すとUIWindowが表示されるものを作っているのですが、表示されるUIwindowの向きが横に表示されます。Device Orientantationはlandscape left,rightに設定してます。
UIAppricationのリファレンスを見たり、ブログなどをみました。shareアプリケーションクラスのメソッドを使いなさいとかNSNotificationCenterにUIApplicationDidChangeStatusBarOrientationNotification通知を受け取る様にしなさいと書いてました。ですが、こちら側の力不足で記事の内容をコードに落とせていません。
わかる人よろしくお願いします。コードは以下の通りです。よろしくお願いします。
//
// ViewController.swift
// rensyu
//
// Created by a on 2015/07/26.
// Copyright (c) 2015年 myconpany. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var mywindow = UIWindow()
var myWindowButton = UIButton()
var mytext:String? = nil
var myTextView = UITextView()
var myAppFrameSize: CGSize?
//ios7以前のサイズに対応
func screenSize() -> CGSize {
let screenSize = UIScreen.mainScreen().applicationFrame.size
if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
return CGSizeMake(screenSize.height, screenSize.width)
}
return screenSize
}
override func viewDidLoad() { super.viewDidLoad() screenSize() // Windowの表示領域すべてのサイズ(pixel). myAppFrameSize = UIScreen.mainScreen().applicationFrame.size var appdelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate appdelegate.myappsize = myAppFrameSize let myScale: CGFloat = UIScreen.mainScreen().scale enum BtnTag: Int { case Zero = 1, One, Two static let allValues = [Zero, One, Two ] } for btag in BtnTag.allValues { var y = CGFloat(60 * btag.rawValue - 1) var btn = UIButton(frame: CGRectMake(0, y, myAppFrameSize!.width, 50)) // サイズを設定する. btn.backgroundColor = UIColor.grayColor() // 背景色を設定する. btn.layer.masksToBounds = true // 枠を丸くする. btn.tag = btag.rawValue btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) switch btn.tag{ // タイトルを設定する // イベントを追加する case 1: btn.setTitle("写真選択", forState: UIControlState.Normal) btn.addTarget(self, action: "callphoto:", forControlEvents: .TouchUpInside) case 2: btn.setTitle("メール", forState: UIControlState.Normal) btn.addTarget(self, action: "callemail:", forControlEvents: .TouchUpInside) case 3: btn.setTitle("window", forState: UIControlState.Normal) btn.addTarget(self, action: "callwindow:", forControlEvents: .TouchUpInside) default: break } self.view.addSubview(btn) } } dynamic func callemail(sender: UIButton){ //mail画面に移動 let secondViewController: second = self.storyboard?.instantiateViewControllerWithIdentifier("secondvc") as second self.presentViewController(secondViewController, animated: true, completion: nil) } // Do any additional setup after loading the view, typically from a nib.
dynamic func callphoto(sender: UIButton){
//photoalbumから写真を選択
let photop = UIImagePickerControllerSourceType.PhotoLibrary if UIImagePickerController.isSourceTypeAvailable(photop){ let picker = UIImagePickerController() picker.sourceType = photop picker.delegate = self self.presentViewController(picker, animated: true, completion: nil) } } //画像選択がキャンセルされた時に呼ばれる. dynamic func imagePickerControllerDidCancel(picker: UIImagePickerController) { // モーダルビューを閉じる self.dismissViewControllerAnimated(true, completion: nil) } //imageviewに保存でjpgにする dynamic func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){ let myAppFrameSize: CGSize = UIScreen.mainScreen().applicationFrame.size var imgi = UIImageView () imgi.frame = CGRectMake(0,0,myAppFrameSize.height,myAppFrameSize.width) let image = info[UIImagePickerControllerOriginalImage] as UIImage self.view.addSubview(imgi) imgi.contentMode = UIViewContentMode.ScaleAspectFit imgi.image = image //画像をNSDataに変換 let data:NSData = UIImageJPEGRepresentation(image,0.99 ) /// 画像ファイルの保存先パスを生成します(ドキュメントフォルダ直下固定)。 var imagePath: String { let doc = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String return doc.stringByAppendingPathComponent("img1.jpg") } //ファイルにデータ書き込み data.writeToFile(imagePath, atomically: true) } dynamic func callwindow(sender: UIButton) { mywindow.frame = CGRectMake(0, 0, myAppFrameSize!.width,myAppFrameSize!.height) myWindowButton.layer.position = CGPointMake(self.mywindow.frame.width/2, self.mywindow.frame.height-50) mytext = "test" mywindow.backgroundColor = UIColor.whiteColor() mywindow.alpha = 0.8 mywindow.layer.cornerRadius = 20 // myWindowをkeyWindowにする. mywindow.makeKeyWindow() self.mywindow.makeKeyAndVisible() myTextView.frame = CGRectMake(10, 10, self.mywindow.frame.width - 20, 150) myTextView.backgroundColor = UIColor.clearColor() myTextView.text = mytext myTextView.font = UIFont.systemFontOfSize(CGFloat(15)) myTextView.textColor = UIColor.blackColor() myTextView.textAlignment = NSTextAlignment.Left myTextView.editable = false self.mywindow.addSubview(myTextView) // } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/13 12:49
2015/08/13 13:38
2015/08/13 15:32
2015/08/13 17:14