Programing

iOS7의 UIAlertView addSubview

lottogame 2020. 11. 10. 07:43
반응형

iOS7의 UIAlertView addSubview


일부 컨트롤 추가 UIAlertViewaddSubview메소드를 사용하여 iOS7에서 더 이상 사용되지 않습니다 . 내가 알다시피 애플은 contentView재산 을 추가하겠다고 약속했다 .

현재 iOS 7 이 출시 되었으며이 속성이 추가되지 않은 것을 확인했습니다. 그래서이 alertView에 진행률 표시 줄을 추가 할 수있는 사용자 지정 솔루션을 검색합니다. 예를 들어 TSAlertView 와 비슷 하지만 iOS 7에서 사용할 수 있습니다.


다음은 iOS7에서 UIAlertView 모양의 대화 상자에 UIView를 추가하는 Github의 프로젝트입니다 .

( 이 StackOverflow 스레드 에서 복사되었습니다 .)

사용자 정의 iOS7 AlertView 대화 상자


Apple과 똑같은 경고보기를 만드는 데 단 하루가 걸렸습니다.

  1. 참조를 위해 Apple의 경고 스크린 샷을 찍습니다 (글꼴 크기, 간격, 너비).
  2. 제목, 메시지, 사용자 정의보기 및 단추 테이블이있는 xib를 만듭니다 (Apple은 UIButton지금 대신 테이블을 사용 하며 기본 테이블 셀이면 충분합니다). 3 개의 버튼 테이블이 필요합니다. 왼쪽 및 오른쪽 버튼 용 2 개 (버튼 수가 2 개일 때마다), 다른 경우 용 (하나의 버튼 또는 2 개 이상의 버튼)
  3. UIAlertView맞춤 알림 에서 모든 메소드를 구현 하세요.

  4. 표시 / 닫기-경고에 대한 특정 모달 창을 만들 수 있지만 루트 뷰 컨트롤러 위에 경고를 넣습니다. 가시적 경고를 정적 배열에 등록하십시오. 첫 번째 경고를 표시하거나 마지막 경고를 해제하는 경우 창 /보기 컨트롤러의 색조 모드를 흐리게 / 자동으로 변경하고 흐리게보기를 추가 / 제거합니다 (알파 = 0.2의 검은 색).

  5. 흐린 배경-Apple의 샘플 코드 사용 (불투명 흰색 사용)
  6. 3D 동적 효과-Apple의 샘플 코드 (5 줄 코드)를 사용합니다. 멋진 효과를 원한다면 5 단계에서 약간 더 큰 스냅 샷을 찍고 경고 배경과 전경에 대해 역 애니메이터를 추가합니다.

편집하다:

흐린 배경과 paralax 효과 샘플 코드는 "iOS_RunningWithASnap"WWDC 2013 샘플 코드에서 찾을 수 있습니다.

Paralax 효과 :

UIInterpolatingMotionEffect* xAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
                                                                                     type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis] autorelease];
xAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
xAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];

UIInterpolatingMotionEffect* yAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
                                                                                     type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis] autorelease];
yAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
yAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];

UIMotionEffectGroup *group = [[[UIMotionEffectGroup alloc] init] autorelease];
group.motionEffects = @[xAxis, yAxis];
[self addMotionEffect:group];

흐린 배경은 유일한 복잡한 것입니다. 대신 불투명 한 색상을 사용할 수 있으면 사용하십시오. 그렇지 않으면 많은 실험이 필요합니다. 또한 배경이 어두울 때 흐린 배경은 좋은 해결책이 아닙니다.

show / dismiss animationg의 경우 새로운 봄 애니메이션 방법을 사용하고 있습니다.

void (^animations)() = ^{
    self.alpha = 1.0f;
    self.transform = CGAffineTransformIdentity;
};

self.alpha = 0.0f;
self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);

[UIView animateWithDuration:0.3
                      delay:0.0
     usingSpringWithDamping:0.7f
      initialSpringVelocity:0.0f
                    options:UIViewAnimationOptionCurveLinear
                 animations:animations
                 completion:^(BOOL completed) {
                         //calling UIAlertViewDelegate method
                     }];

완전한 UIAlertView API를 모방 한 UIAlertView의 전체 구현을 작성했지만 우리 모두가 오랫동안 원했던 contentView 속성 인 SDCAlertView를 추가했습니다 .

영상


For those who love simple and effective methods with out having to write lines of code. Here is a cool solution without using any other private frame works for adding subviews to ios 7 alert views,i.e.

[alertView setValue:imageView forKey:@"accessoryView"];

Sample code for better understanding,

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
UIImage *wonImage = [UIImage imageNamed:@"image.png"];
[imageView setImage:wonImage];

//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
      [alertView setValue:imageView forKey:@"accessoryView"];
}else{
      [alertView addSubview:imageView];
}

Hope it helps some one,thanks :)


For IOS7

UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"Enter Form Name" 
                                               message:@""
                                               delegate:self 
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"Ok", nil];
alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *myTextField = [alertView1 textFieldAtIndex:0];
[alertView1 setTag:555];
myTextField.keyboardType=UIKeyboardTypeAlphabet;

[alertView1 show];

There wont be UIAlertView with custom views in iOS7, nor contentView which Apple changed its mind about, so addSubview is impossible now in UIAlertView.

A good alternative will be SVProgressHUD, according to many threads in Apple's forum.

Edit:

There is officially no addSubview nor subclassing for UIAlertView in iOS7.

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Other good alternatives:

ios-custom-alertview by wimagguc

MZFormSheetController.


You can find simple solution without extra classes here

It is based on setting accessoryView for ordinary UIAlertView.


PKAlertController (https://github.com/goodpatch/PKAlertController) is great library. I tested a lot of similar libraries and just this satisfied all my requirements.

Why it is cool:

  • Supports custom view
  • Supports iOS7
  • It is view controller
  • It behaves and looks like native alert view, including motion effects
  • Customizable
  • Similar interface like in UIAlertController

참고 URL : https://stackoverflow.com/questions/18729220/uialertview-addsubview-in-ios7

반응형