سؤال

في وحدة تحكم الجذر عرض الجدول الأول إضافة subview الذي يحمل صورة:

[self.navigationController.navigationBar addSubview:imageView];

وبعد ذلك في وحدة تحكم عرض الجدول التابع أنني دفع إلى المكدس احدد UIBarButtonItem اليمين:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Right"  style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

لا أستطيع أن أفهم لماذا لا يتم عرض زر في شريط التنقل. إذا كنت الصنبور (المساحة الفارغة) وrightAction: يحصل الاحتجاج طريقة لذلك على زر "هو" هناك، ونتوقع أن لا يتم عرضها

ولقد حاولت أن ترسل subview imageView إلى الوراء في شريط التنقل ولكن دون جدوى. انها نوع من المنطقي منذ زر ينتمون فعلا إلى UINavigationItem على أي حال ...

هل أحد يعرف كيف يمكن أن تجعل هذا الزر العرض بشكل صحيح؟

تحديث: لم يعرض على الزر مرة أخرى بشكل صحيح ولكن يتم إضافته من قبل النظام على الرغم من ...

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

المحلول

وهناك طريقة أفضل لإضافة صورة إلى UINavigationBar هي فئة فرعية أو جعل فئة وتجاوز أسلوب drawRect:

//                                  #Lighter r,g,b,a            #Darker r,g,b,a
#define MAIN_COLOR_COMPONENTS       { 0.153, 0.306, 0.553, 1.0, 0.122, 0.247, 0.482, 1.0 }
#define LIGHT_COLOR_COMPONENTS      { 0.478, 0.573, 0.725, 1.0, 0.216, 0.357, 0.584, 1.0 }

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    if (imageReady) {
        UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
        [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    } else {
        // Render yourself instead.
        // You will need to adjust the MAIN_COLOR_COMPONENTS and LIGHT_COLOR_COMPONENTS to match your app

       // emulate the tint colored bar
       CGContextRef context = UIGraphicsGetCurrentContext();
       CGFloat locations[2] = { 0.0, 1.0 };
       CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();

       CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
       CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
       CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
       CGGradientRelease(topGradient);

       CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
       CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
       CGContextDrawLinearGradient(context, botGradient,
       CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
       CGGradientRelease(botGradient);

       CGColorSpaceRelease(myColorspace);


       // top Line
       CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
       CGContextMoveToPoint(context, 0, 0);
       CGContextAddLineToPoint(context, self.frame.size.width, 0);
       CGContextStrokePath(context);

       // bottom line
       CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
       CGContextMoveToPoint(context, 0, self.frame.size.height);
       CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
       CGContextStrokePath(context);
    }
}

@end

وفعل ذلك بهذه الطريقة سوف يسبب أزرار لتظهر بشكل صحيح وأقل "hacky".

نصائح أخرى

وأعتقد أن عليك أن تحدد عرض عنوان العنصر الملاحة لimageView الخاص بك.

self.navigationItem.titleView = imageView;

واعتقد ان UINavigationBar يعرض UINavigationItems في subview، وكنت إضافة UIImageView الخاص بك على رأس ذلك subview.

في واحد من وجهة نظري أنا عرض تسمية وصورة بجانبه. أعتقد أنا فقط واضاف وجهة التسمية والصورة إلى الرأي الذي ثم استخدم لtitleView.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top