2012-03-14 9 views
5

मैं दो बटन के साथ UIAlertView बनाना चाहता हूं। मुझे इन बटनों को लंबवत रूप से रखना होगा (मेरा टेक्स्ट बहुत बड़ा है)। क्या यह संभव है ?क्या UIAlertView पर लंबवत 2 बटन रखना संभव है?

Screen1

enter image description here

screen2

enter image description here

+0

और आपका कोड ऐसा लगता है? – nycynik

उत्तर

4

डिफ़ॉल्ट रूप से यह संभव नहीं है। Nycynik के जवाब में दिखाया गया दिखावट तब होता है जब आपके पास दो से अधिक बटन होते हैं।

तो, या तो एक और बटन जोड़ें, या आप this library जैसे किसी तृतीय-पक्ष समाधान में देख सकते हैं, हालांकि मैंने इसका परीक्षण नहीं किया है।

-3

हाँ, यहाँ के साथ कोड & ट्यूटोरियल एक पृष्ठ है:

http://mobile.tutsplus.com/tutorials/iphone/uialertview/

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!" 
                message:@"This is your first UIAlertview message." 
               delegate:self 
             cancelButtonTitle:@"Button 1" 
             otherButtonTitles:@"Button 2", @"Button 3", nil]; 
[message show]; 
+2

लिंक किए गए ट्यूटोरियल में दिखाए गए स्टैक्ड बटन इसलिए हैं क्योंकि तीन बटन हैं। वोल्डा 2 में केवल दो बटन हैं, इसलिए वे डिफ़ॉल्ट रूप से क्षैतिज रूप से ढेर होते हैं। –

0

आवश्यक स्थान बनाने के लिए आप कई लाइनब्रेक "\ n \ n \ n" के साथ प्लेसहोल्डर संदेश का उपयोग कर सकते हैं। AlertView के शीर्ष पर एक कस्टम बटन जोड़ने से भी। यह निश्चित रूप से हैक - एक बहुत ठोस समाधान नहीं है। लेकिन एक दूसरे के शीर्ष पर दिखाई देने के लिए केवल दो बटनों के लिए कोई सीधा आगे समाधान नहीं है। तीन बटन या उससे अधिक के साथ यह वैसे भी डिफ़ॉल्ट व्यवहार है।

0

आप प्रयास करना चाहिए:

UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Message" 
    delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Download Next Chapter", nil]; 
    [dialog show]; 
-3

इस का उपयोग करें:

UIAlertView *alert = [[[UIAlertView alloc] 
         initWithTitle:@"Title" 
         message:@"Two Vertical Button" 
         delegate:nil 
         cancelButtonTitle:@"" 
         otherButtonTitles:@"Button 1", @"Button 2", 
         nil] 
        autorelease]; 
[[alert viewWithTag:1] removeFromSuperview]; 
[alert show]; 
+0

अपने उत्तर की गुणवत्ता में सुधार करने के लिए, कृपया अपनी पोस्ट समस्या को हल करने के लिए/क्यों शामिल करें। –

+0

यह सिर्फ एक खाली रद्द करें बटन ... और दो अन्य बटन बनाता है। –

8

इस व्यवहार के लिए कोई एसडीके समर्थन हो रहा है। UIAlertView में दो बटन हमेशा एक क्षैतिज लेआउट में दिखाए जाएंगे।

हालांकि, इच्छित व्यवहार प्राप्त करने के लिए UIAlertView को उपclass करना बहुत आसान है। आइए कक्षा वर्टिकल अलर्ट व्यू पर कॉल करें।

निम्नलिखित कोड केवल दो बटनों के साथ अलर्ट दृश्यों के लिए काम करता है क्योंकि में दो से अधिक बटन UIAlertView स्वचालित रूप से लंबवत लेआउट में दिखाए जाएंगे।

VerticalAlertView.h इस के रूप में सरल है:

#import <UIKit/UIKit.h> 

@interface VerticalAlertView : UIAlertView 
@end 

VerticalAlertView।मीटर:

#import "VerticalAlertView.h" 

@implementation VerticalAlertView 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    int buttonCount = 0; 
    UIButton *button1; 
    UIButton *button2; 

    // first, iterate over all subviews to find the two buttons; 
    // those buttons are actually UIAlertButtons, but this is a subclass of UIButton 
    for (UIView *view in self.subviews) { 
     if ([view isKindOfClass:[UIButton class]]) { 
      ++buttonCount; 
      if (buttonCount == 1) { 
       button1 = (UIButton *)view; 
      } else if (buttonCount == 2) { 
       button2 = (UIButton *)view; 
      } 
     } 
    } 

    // make sure that button1 is as wide as both buttons initially are together 
    button1.frame = CGRectMake(button1.frame.origin.x, button1.frame.origin.y, CGRectGetMaxX(button2.frame) - button1.frame.origin.x, button1.frame.size.height); 

    // make sure that button2 is moved to the next line, 
    // as wide as button1, and set to the same x-position as button1 
    button2.frame = CGRectMake(button1.frame.origin.x, CGRectGetMaxY(button1.frame) + 10, button1.frame.size.width, button2.frame.size.height); 

    // now increase the height of the (alert) view to make it look nice 
    // (I know that magic numbers are not nice...) 
    self.bounds = CGRectMake(0, 0, self.bounds.size.width, CGRectGetMaxY(button2.frame) + 15); 
} 

@end 

अब आप किसी अन्य UIAlertView की तरह अपने वर्ग का उपयोग कर सकते हैं:

[[[VerticalAlertView alloc] initWithTitle:@"Title" 
            message:@"This is an alert message!" 
           delegate:self 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:@"Second Button", nil] autorelease] show]; 

और आप निम्न परिणाम प्राप्त होगा:

enter image description here

संपादित करें:

इस विधि का उपयोग करना थोड़ा जोखिम भरा है (हैकी कहने के लिए नहीं), क्योंकि ऐप्पल कुछ बिंदु पर UIAlertView के कार्यान्वयन को बदल सकता है, जो आपके लेआउट को तोड़ सकता है। मैं बस यह इंगित करना चाहता था कि यह आपकी समस्या के लिए एक आसान और तेज़ समाधान होगा। के रूप में UIAlertView संदर्भ में उल्लेख किया है:

"UIAlertView वर्ग के रूप में-है इस्तेमाल किया जा करने का इरादा नहीं है और समर्थन उपवर्गीकरण इस वर्ग के लिए दृश्य पदानुक्रम निजी है और संशोधित नहीं होना चाहिए करता है।।"

+0

यह आईओएस 7.0 एसडीके के लिए काम नहीं कर रहा है। क्या आप कृपया आईओएस 7 के साथ कुछ @ एफएल –

+1

सुझाएंगे, ऐप्पल ने यूआईएलर्टव्यू को उप-वर्गीकृत करना लगभग असंभव बना दिया है, लेकिन इसे किसी भी तरह से उप-वर्गीकृत करने का इरादा नहीं था। अब अलर्ट में कस्टम सबव्यू जोड़ना भी संभव नहीं है, आपको इसे करने के लिए यहां वर्णित विधि का उपयोग करना होगा: http://stackoverflow.com/a/21067447/378977। हालांकि, दुर्भाग्यवश, मुझे अभी तक आईओएस 7 पर दो लंबवत स्टैक्ड बटनों का समाधान नहीं मिला। मुझे लगता है कि इसके लिए अपनी खुद की कस्टम कक्षा बनाने के लिए कोई रास्ता नहीं है। तो शायद [इयान एल] (https: // stackoverflow द्वारा सुझाए गए अनुसार TSAlertView पर एक नज़र देखें।कॉम/उपयोगकर्ता/596115/ian-l) और आईओएस 7 के लिए इसे प्रयोग योग्य बनाते हैं। – Flo

0

UIAlertView प्रतिनिधियोंios 9,0

इसके अलावा जब आप अधिक तो 2 बटन जोड़ने के लिए, वे खड़ी आईओएस द्वारा आवंटित किया जाएगा में पदावनत कर रहे हैं।

UIAlertController के लिए UIAlertAction रों बनाते समय आप बस UIAlertController

UIAlertController * alert= [UIAlertController 
            alertControllerWithTitle:[[[NSBundle mainBundle] infoDictionary] 
                  objectForKey:@"CFBundleDisplayName"] 
            message:@"share via" 
            preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* fbButton = [UIAlertAction 
           actionWithTitle:@"Facebook" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) 
           { 
            // Add your code 
           }]; 
    UIAlertAction* twitterButton = [UIAlertAction 
            actionWithTitle:@"Twitter" 
            style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) 
            { 
             // Add your code 

            }]; 
    UIAlertAction* watsappButton = [UIAlertAction 
            actionWithTitle:@"Whatsapp" 
            style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) 
            { 
             // Add your code 
            }]; 
    UIAlertAction* emailButton = [UIAlertAction 
            actionWithTitle:@"Email" 
            style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) 
            { 

             // Add your code 
            }]; 
    UIAlertAction* cancelButton = [UIAlertAction 
            actionWithTitle:@"Cancel" 
            style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) 
            { 
             //Handel no, thanks button 

            }]; 

    [alert addAction:fbButton]; 
    [alert addAction:twitterButton]; 
    [alert addAction:watsappButton]; 
    [alert addAction:emailButton]; 
    [alert addAction:cancelButton]; 

    [self presentViewController:alert animated:YES completion:nil]; 

IOS 9 Alert with vertical buttons

1

साथ कर सकते हैं, बस खिताब के लिए कुछ सफेद स्थान जोड़ देते हैं। भले ही उन्हें प्रदर्शित करते समय छंटनी की जाएगी, आईओएस लंबवत बटन देता है।

4.7 के साथ आईओएस 10.3.1 पर परीक्षण किया गया "डिवाइस अंतरिक्ष की यह राशि पर्याप्त है यहां तक ​​कि एक बटन शीर्षक 1 वर्ण लंबाई के लिए:।

NSString* space = @" ";

अधिकांश शायद यह 3.5 के लिए ठीक है", 4 "और 5.5" डिवाइस भी।

संबंधित मुद्दे