مشكلة إضافة صورة إلى شريط الأدوات باستخدام uibarbuttonitem ، وعرض مربع أبيض فارغ بدلاً من الصورة
-
26-09-2019 - |
سؤال
لست متأكدًا مما أفعله خطأ. اسم الملف صحيح ، يتم تعيين النمط على عادي. لكنني أحصل على صندوق أبيض بنك بحجم صورتي. أنا أستخدم UinavigationController.
الرجاء المساعدة وشكرا شكرا لك مقدما.
** FYI أنا جديد نوعًا ما في الهدف ، لذا لا تكون صعبًا جدًا علي. ؛)
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"channel-guide-button.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(action:)];
self.toolbarItems = [NSArray arrayWithObjects:toolbarChannelGuideButton, nil];
[toolbarChannelGuideButton release];
المحلول
السبب في أنه كان يخلق القناع الأبيض هو أن UIToolBar
لا تسمح الصور الملونة عليه افتراضيًا. الطريقة لإنجاز هذا هي إنشاء ملف UIImage
ثم تعيين أ UIButton
لتلك الصورة. ثم إنشاء UIBarButton
استخدام initWithCustomView
مع ال UIButton
كما العرض المخصص.
شفرة:
//Load the image
UIImage *buttonImage = [UIImage imageNamed:@"your-image.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//sets the frame of the button to the size of the image
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//creates a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
[customBarItem release];
نصائح أخرى
بدءًا من نظام التشغيل iOS 7 ، يمكنك استخدامه أدناه:
UIImage *image = [[UIImage imageNamed:@"myImage.png"];
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
هل تنتمي قناة توجيه button.png إلى Project؟
يمكنك كسر هذا مثل هذا:
UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"];
NSLog(@" image = %p", image);
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
initWithImage:image
style:UIBarButtonItemStylePlain
target:self
action:@selector(action:)];
أو فقط تحقق من مشروعك ؛-)
لا تنتمي إلى StackOverflow