كيف يمكنك إضافة أكثر من uibarbutton على uinavigationitem.rightbarbuttonitem (أو LeftBarButtonitem)؟

StackOverflow https://stackoverflow.com/questions/1414510

سؤال

لقد جربت هذا النهج/الاختراق:http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/

المشكلة هي أن هذا يترك التماس الخافت. حاولت تعيين صورة الخلفية لشريط الأدوات المتداخلة على صورة التقطتها مما يجب أن يكون. هذا لم ينجح. لم يتم تطبيق الصورة. لقد حاولت أيضًا استخدام uinavigationbar المتداخل وهذا لا يبدو أنه يعمل.

لقد رأيت ذلك في العديد من تطبيقات iPhone. لا أحد يعرف كيف؟

تحرير] أريد أن تبدو الأزرار وكأنها uibarbuttonitems العادية وتكون قادرة على استخدام أنماط النظام مثل uibarbuttonsystemitemadd ، uibarbuttonsystemitemrefresh. الرابط الذي قدمته يفعل ذلك إلا أنه يمكنك رؤية التماس الخافت لأنه uitoolbar متداخل في NavigationBar ..

من فضلك لا تذكر هذا كسر إرشادات الواجهة البشرية. (نعلم).

أنا أقدر لك المساهمة باختراقاتك ... هذه هي الطريقة الوحيدة للقيام بذلك!

هل كانت مفيدة؟

المحلول

للتخلص من الخلفية ('seam') من uitoolbar ، قم بإنشاء فئة فرعية من uitoolbar وتجاوز طريقة RECT (cGRECT) (cGRECT). اترك هذا فارغًا ولن يكون لدى uitoolbar خلفية.

استخدمت هذا للتو في مشروعي الخاص وعملت بشكل رائع. وجدت هذا في تعليقات: http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

نصائح أخرى

iOS 5.0 يدعم الآن أزرار متعددة. راجع وثائق iOS لـ UinavigationItem. على وجه التحديد ، ما يلي:

الخصائص:

@property(nonatomic, copy) NSArray *leftBarButtonItems;
@property(nonatomic, copy) NSArray *rightBarButtonItems;
@property BOOL leftItemsSupplementBackButton;

طُرق:

- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;

أنا نشرت رمز لإضافة زرين على يمين javigationbar. يمكنك ضبط barStyle = -1 بدلا من التصنيف الفرعي UIToolbar.

UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight)];
// make UIView customView1... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView1];
[customView1 release];
// make UIView customView2... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView2];
[customView2 release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];

شاهد مثال Uicatalogue المتاح في موقع Apple مجانًا ... لقد استخدموا التحكم في Uisegtess لإظهار ثلاثة أزرار بدلاً من زر الشريط الأيمن على شريط Navigaion ...

لا يمكنني التعليق ، لكن بالإضافة إلى iworkinprogress ، اضطررت إلى ضبط لون خلفية Uitoolbar لمسح:

    [toolbar setBackgroundColor:[UIColor clearColor]];

تم العثور على هذا أيضًا في تعليقات http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html.

في iOS 4.x ، يبدو أن ClearColor ليس له أي تأثير على Uitoolbar ، في حين أن التغلب على السحب: فعل.

توصلت إلى وظيفة مساعد أستخدمه في جميع أنحاء مشروعي. في الأساس ، يتحقق ما إذا كان هناك زر بالفعل على الشريط وإضافة واحد جديد أو دمجه مع الأزرار الموجودة. لذلك يمكنك استدعاء الوظيفة مرة واحدة فقط أو عدة مرات:

+ (void)AddButtonToBar:(UIViewController *)controller withImage:(NSString *)imageName withAction:(SEL)action withFrame:(CGRect) frame{
    UIButton *newButton =[[UIButton alloc] init];
    [newButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    newButton.frame = frame;
    [newButton addTarget:controller action:action forControlEvents:UIControlEventTouchUpInside];

    if ([[controller.navigationItem rightBarButtonItems] count] == 0)
        [controller.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
    else {
        NSMutableArray *existingButtons = [[NSMutableArray alloc] initWithArray:[controller.navigationItem rightBarButtonItems]];
        [existingButtons addObject:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
        [controller.navigationItem setRightBarButtonItems:(NSArray *)existingButtons];
    }
}

نسميها من وحدة التحكم في العرض:

[Helper AddButtonToBar:self withImage:@"imageName.png" withAction:@selector(myAction) withFrame:CGRectMake(0, 0, 24, 24)];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top