4

だから私はUIButtonそれがクリックされた後に移動しようとしています。

_addMoreFieldsボタンがクリックされた後、メソッドが呼び出されます。

_addMoreFieldBtnグローバルUIButtonです。クリックしても何も起こりません。

addSubView奇妙な部分は、コードをコメントアウトするとボタンが動くことです。

そのコードを保持すると、ボタンは移動しません。

何か案は?

-(void)movePlusButton {
    NSLog(@"Moving button");
    [UIButton beginAnimations:nil context:nil];
    [UIButton setAnimationDuration:0.3];
    _addMoreFieldsBtn.center = CGPointMake(30,30);
    [UIButton commitAnimations];
}

- (IBAction)addMoreFields:(id)sender {

    CGRect currentBtnFrame = [(UIButton *)sender frame];
    CGPoint org = currentBtnFrame.origin;

    UILabel *whoWasIn = [[UILabel alloc]  initWithFrame:CGRectMake(110, org.y, 85, 21)];
    whoWasIn.text = @"test";

    UITextField *whoWasInField = [[UITextField alloc] initWithFrame:CGRectMake(59, whoWasIn.frame.origin.y+40, 202, 30)];
    whoWasInField.placeholder = @"test2";

    UILabel *with = [[UILabel alloc]  initWithFrame:CGRectMake(136, whoWasInField.frame.origin.y+40, 49, 21)];
    with.text = @"with";
    whoWasInField.borderStyle = UITextBorderStyleRoundedRect;

    UITextField *withField = [[UITextField alloc] initWithFrame:CGRectMake(59, with.frame.origin.y+40, 202, 30)];
    withField.placeholder = @"test3";
    withField.borderStyle = UITextBorderStyleRoundedRect;

    [_homeView addSubview:whoWasIn];
    [_homeView addSubview:with];
    [_homeView addSubview:whoWasInField];
    [_homeView addSubview:withField];

    [self movePlusButton];
}

注:フレームを変更しようとしましたが、同じ問題が発生します。新しい場所から既存の場所にアニメーションを開始します。

4

2 に答える 2

5

問題は、iOS 6 /Xcode4.5の新しいプロジェクトでデフォルトで「自動レイアウト」が有効になっていることです。自動レイアウトは「SpringsandStruts」に代わるものです(ただし、iOS 6でのみ機能します)。この機能は、コードで試みた移動よりも優先される制約をビューに追加します。

したがって、これには3つの可能な修正があります。

1)プログラムでボタンに新しい制約を作成します。自動レイアウトは非常に強力で柔軟性があります...特にiPhone5とそれ以前のモデルの両方のフットプリントをサポートしようとしている場合はそうです。これを行う方法の詳細については、WWDCビデオをご覧ください:iOSおよびOSXの自動レイアウトの概要

2)自動レイアウトを使用しないでください。ストーリーボードでビューを選択し、ファイルインスペクターで[自動レイアウトを使用する]のチェックを外します。

3)ボタンの制約ごとにIBOutletsを作成します。次に、ボタンを移動する前に、これらの制約を削除します。

@interface MyViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *addMoreFieldsBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *vConstraint;
- (IBAction)addMoreFields:(id)sender;
@end

と...

-(void)movePlusButton {
    NSLog(@"Moving button");
    [self.view removeConstraint:self.hConstraint];
    [self.view removeConstraint:self.vConstraint];
    [UIButton beginAnimations:nil context:nil];
    [UIButton setAnimationDuration:0.3];
    _addMoreFieldsBtn.center = CGPointMake(30,30);
    [UIButton commitAnimations];
}

(呼び出す必要のある実際のビューremoveConstraints:は、ボタンの親ビューであり、self.viewである場合とそうでない場合があります)。

于 2012-10-26T03:33:04.237 に答える
0

実際、何が起こっているのかというと、サブビューが最初に画面に表示されるため、優先されます。次に、サブビューが削除されると、コードを変更するとボタンが移動します。

[_homeView addSubview:whoWasIn];
[_homeView addSubview:with];
[_homeView addSubview:whoWasInField];
[_homeView addSubview:withField];

[self movePlusButton];

}//[自己movePlusButton]を移動します; 6行のコードを増やすか、最初の行にします

[self movePlusButton];
[_homeView addSubview:whoWasIn];
[_homeView addSubview:with];
[_homeView addSubview:whoWasInField];
[_homeView addSubview:withField];

これですべての問題が解決するはずです

:)

于 2012-10-28T17:26:59.833 に答える