ラベル objective-c の投稿を表示しています。 すべての投稿を表示
ラベル objective-c の投稿を表示しています。 すべての投稿を表示

2014年5月27日火曜日

UIView:枠線をつける・角丸をつける

UIViewに枠線をつける。
・Link Binary With LibrariesにQuartzCore.frameworkを追加する。
・QuartzCore/QuartzCore.h をインポートする。

// 枠線の幅
view.layer.borderWidth = 1.0f;
// 枠線の色
view.layer.borderColor = [[UIColor lightGrayColor] CGColor];
// 角丸をつける場合の半径
view.layer.cornerRadius = 8.0f;

2014年5月19日月曜日

UITextField : 縦方向のアライメントを中央揃えにする

UITextFieldの縦方向のアライメントを中央揃えにするにはcontentVerticalAlignmentにUIControlContentVerticalAlignmentCenterを設定する。

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

2014年5月16日金曜日

iTunesとのファイル共有の設定

iTunesのAppのファイル共有でファイルを転送できるようにするには、info.plistを以下のように設定する。

Application supports iTunes file sharing : YES

2014年5月14日水曜日

Documentsディレクトリの取得

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

2014年5月5日月曜日

2014年5月4日日曜日

UISliderのカスタマイズ

UISliderのつまみ(ThumbImage)の画像を変更するには setThumbImage:forState: を使用する。
背景の画像を変更するには、setMinimumTrackImage:forState: , setMaximumTrackImage:forState: を使用する。

// つまみの画像
[_slider setThumbImage:[UIImage imageNamed:@"handle3.png"]
              forState:UIControlStateNormal];
[_slider setThumbImage:[UIImage imageNamed:@"handle3.png"]
              forState:UIControlStateHighlighted];
// 背景のバーの画像(最小値側)
[_slider setMinimumTrackImage:[UIImage imageNamed:@"bg1.jpg"]
                     forState:UIControlStateNormal];
// 背景のバーの画像(最大値側)
[_slider setMaximumTrackImage:[UIImage imageNamed:@"bg2.jpg"]
                     forState:UIControlStateNormal];

2014年5月2日金曜日

UITableView : 選択不可にする

UITableViewを選択不可にするにはプロパティallowsSelectionをNOにする。

self.tableView.allowsSelection = NO;

2014年5月1日木曜日

アプリアイコンの光沢を消す

iOS7ではデフォルトでアプリアイコンの光沢(グロス)はついていない。
xcassetsを使用する場合で、iOS6以前のアプリアイコンの光沢(グロス)を消す場合は、
XcodeでxcassetsのAppIconを選択し、右側のAttributes inspectorを表示して iOS icon is pre-rendered にチェックを入れて、一旦クリーンしてビルドし直す。

xcassetsを使用しない場合は、info.plistで Icon already includes gloss effects を YES に設定する。

Xcode : ワーニング A 57x57 app icon is required for iPhone apps targeting releases of iOS prior to 7.0

iOS6以前のバージョンをサポートしているアプリで、57×57や114×114のアプリアイコンを設定していないと、以下のようなワーニングがでる。

A 57x57 app icon is required for iPhone apps targeting releases of iOS prior to 7.0
A 57x57@2x app icon is required for iPhone apps targeting releases of iOS prior to 7.0

このときに、xcodeでxcassetsにアイコンを追加しようとしても、iOS6以前のアプリアイコンサイズを追加する欄がないときは、
xcassetsのAppIconを選択して、右側のAttributes inspectorを表示し、iOS 6.1 and Prior Sizes にチェックを入れる。


2014年4月30日水曜日

Retinaディスプレイの判定

Retinaディスプレイかどうかの判定には、[UIScreen mainScreen].scale の値を見る。

if ([UIScreen mainScreen].scale == 2.0) {
// retinaディスプレイ
}

2014年4月29日火曜日

Xcode : The app icon set named "AppIcon" did not have any applicable content.

Xcodeで

The app icon set named "AppIcon" did not have any applicable content.

のエラーがでる場合、アイコンのサイズが間違っている。

例えば、Images.xcassetsでAppIconのiPhone App iOS7 60ptのアイコンに60×60のアイコンを設定した場合など。
2xなので、120×120のアイコンを設定する。

ステータスバーをタップしてもUITableView, UIScrollViewをTOPにしない

UITableView, UIScrollViewはデフォルトではステータスバーをタップした時に一番上まで戻る。
これを無効にするにはプロパティscrollsToTopをNOにする。

self.tableView.scrollsToTop = NO;

2014年4月28日月曜日

UITableView : セパレータを端までのばす

iOS7.Xのデフォルトでは、UITableViewのセパレータの左端とViewの左端の間隔があいている。
この間隔をなくして、セパレータをUITableViewの左端までのばす。

// iOS7のセパレータを端までのばす
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}

2014年4月27日日曜日

UISliderを作成する

@interface SliderViewController ()
@property (strong, nonatomic) UISlider *slider;
@end

@implementation SliderViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    // スライダーの作成
    _slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
    // 最小値
    _slider.minimumValue = 0.0f;
    // 最大値
    _slider.maximumValue = 1.0f;
    // 現在値
    _slider.value = 0.5f;
    // スライダー変更時のメソッド
    [_slider addTarget:self
                action:@selector(onChangeSlider:)
      forControlEvents:UIControlEventValueChanged];
    // スライダーをviewに追加
    [self.view addSubview:_slider];
}

// スライダー変更時
- (void)onChangeSlider:(id)sender
{
    UISlider *slider = (UISlider*)sender;
    NSLog(@"%f", slider.value);
}

UITableView : 表示位置を変更する

UITableViewを特定のセルまでスクロールさせて表示位置を変更するには、 scrollToRowAtIndexPath:atScrollPosition:animated: を使用する。

// 表示するセルのindexPath作成
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:_index inSection:0];

// 指定セルまでスクロール
// atScrollPosition: UITableViewScrollPositionNone   移動量少なくTableViewの表示に入るように
//                   UITableViewScrollPositionTop    TableViewの表示の先頭にスクロール
//                   UITableViewScrollPositionMiddle TableViewの表示の真ん中にスクロール
//                   UITableViewScrollPositionBottom TableViewの表示の末尾にスクロール
[self.tableView scrollToRowAtIndexPath:indexPath
                      atScrollPosition:UITableViewScrollPositionMiddle
                              animated:YES];

2014年4月26日土曜日

Xcode : ワーニング Writable atomic property 'page' cannot pair a synthesized getter with a user defined setter

ワーニング

Format string is not a string literal (potentially insecure)

がでる場合、NSLogなどでフォーマットを指定していない。

NSString *str = [NSString stringWithFormat:@"%.02f", width];
NSLog(str);

NSString *str = [NSString stringWithFormat:@"%.02f", width];
NSLog(@"%@", str);

2014年4月19日土曜日

SLComposeViewControllerを使用したTwitter, Facebook投稿

プロジェクトにSocial.framework を追加する。

Social.hをインポートする。


・Twitter投稿の場合
SLComposeViewControllerでTwitter投稿画面を表示する。
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
       SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
       // CompletionHandlerを設定
       [composeVC setCompletionHandler:^(SLComposeViewControllerResult result) {
           [self dismissViewControllerAnimated:YES completion:nil];
           if (result == SLComposeViewControllerResultDone) {
               NSLog(@"sendTwitter success");
           }
       }];

       // POSTするテキストの初期設定
       NSString* message = [NSString stringWithFormat:@"XXXXXXXXXX"];
       [composeVC setInitialText:message];
       // URLをPOSTする場合
       [composeVC addURL:[NSURL URLWithString:@"http://XXXXXXXX"]];
       // 画像をPOSTする場合
       [composeVC addImage:[UIImage imageNamed:@"XXXXX"]];
       // SLComposeViewController表示
       [self presentViewController:composeVC animated:YES completion:nil];
   }
CompletionHandlerに入れたdismissViewControllerAnimatedはiOS7.Xではなくても動いたが、
iOS6.Xでは入れないと投稿後の画面操作ができなくなる。



・Facebook投稿の場合
SLComposeViewControllerでFacebook投稿画面を表示する。
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
       SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
       [composeVC setCompletionHandler:^(SLComposeViewControllerResult result) {
           [self dismissViewControllerAnimated:YES completion:nil];
           if (result == SLComposeViewControllerResultDone) {
               DEBUG_LOG(@"sendFacebook success");
           }
       }];

       // POSTするテキストの初期設定
       NSString* message = [NSString stringWithFormat:@"XXXXXXXXXX"];
       [composeVC setInitialText:message];
       // URLをPOSTする場合
       [composeVC addURL:[NSURL URLWithString:@"http://XXXXXXXX"]];
       // 画像をPOSTする場合
       [composeVC addImage:[UIImage imageNamed:@"XXXXX"]];
       // SLComposeViewController表示
       [self presentViewController:composeVC animated:YES completion:nil];
   }
CompletionHandlerに入れたdismissViewControllerAnimatedはなくても動く。

2014年4月13日日曜日

デバイスの情報を取得する

[UIDevice currentDevice]からOSバージョンやiPadかiPhoneか等、デバイスの情報を取得する。

デバイスのUDIDはかつては取得できたが、現在は取得できなくなった。

// 名称
NSLog(@"name[%@]", [UIDevice currentDevice].name);
// OS名
NSLog(@"systemName[%@]", [UIDevice currentDevice].systemName);
// OSバージョン
NSLog(@"systemVersion[%@]", [UIDevice currentDevice].systemVersion);
// デバイスのモデル
NSLog(@"model[%@]", [UIDevice currentDevice].model);
NSLog(@"localizedModel[%@]", [UIDevice currentDevice].localizedModel);
// デバイスのインターフェースタイプ(iPhoneかiPadか)
// UIUserInterfaceIdiomPhone, UIUserInterfaceIdiomPad
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    NSLog(@"userInterfaceIdiom is iPad");
}
else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
    NSLog(@"userInterfaceIdiom is iPhone");
}
// アプリのベンダー毎のUUID
NSLog(@"identifierForVendor[%@]", [UIDevice currentDevice].identifierForVendor);


2014-04-13 13:42:21.503 Device[5307:60b] name[XXXのiPad]
2014-04-13 13:42:21.505 Device[5307:60b] systemName[iPhone OS]
2014-04-13 13:42:21.506 Device[5307:60b] systemVersion[7.0.6]
2014-04-13 13:42:21.507 Device[5307:60b] model[iPad]
2014-04-13 13:42:21.509 Device[5307:60b] localizedModel[iPad]
2014-04-13 13:42:21.510 Device[5307:60b] userInterfaceIdiom is iPad
2014-04-13 13:42:21.515 Device[5307:60b] identifierForVendor[<__nsconcreteuuid 0x15604e20> D291C209-B5AF-430A-9AC5-66859F0AF1CB]


※identifierForVendorはデバイス上の対象ベンダーのアプリ全てを削除して、再インストールすると別IDになる。

デバイスの回転対応

iOS7以降でデバイスの回転に対応するには、
XcodeのTARGETS->GeneralでDeployment Info->Device Orientation(info.plistのSupported interface orientations)でサポートする向きを設定しておいて、
ViewControllerに以下の処理を入れる。

// 回転するかどうか
- (BOOL)shouldAutorotate
{
    // 回転したときのViewの配置等の処理を記述

    // 回転する場合YES, しない場合NO
    return YES;
}

// サポートするインターフェースの向き
- (NSUInteger)supportedInterfaceOrientations
{
    // UIInterfaceOrientationMaskPortrait : 縦(ホームボタン下)
    // UIInterfaceOrientationMaskLandscapeLeft : 横(ホームボタン左)
    // UIInterfaceOrientationMaskLandscapeRight : 横(ホームボタン右)
    // UIInterfaceOrientationMaskPortraitUpsideDown : 縦(ホームボタン上)
    // UIInterfaceOrientationMaskLandscape : 横(両方)
    // UIInterfaceOrientationMaskAll : 4つ全て
    // UIInterfaceOrientationMaskAllButUpsideDown : 縦(ホームボタン上)以外の3つ
    return UIInterfaceOrientationMaskLandscape;
}

// インターフェースの向き(初期状態)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    NSLog(@"preferredInterfaceOrientationForPresentation");
    
    // UIInterfaceOrientationPortrait : 縦(ホームボタン下)
    // UIInterfaceOrientationPortraitUpsideDown : 縦(ホームボタン上)
    // UIInterfaceOrientationLandscapeLeft : 横(ホームボタン左)
    // UIInterfaceOrientationLandscapeRight : 横(ホームボタン右)
    return UIInterfaceOrientationLandscapeRight;
}