سؤال

لذلك لدي تطبيق iPhone هذا مع إعداد متداخل مثل هذا: [RootViewController]-> [uitabbarcontroller]-> [uinavigationController]-> [SubviewControllers]. تم تصميم كل شيء برمجيًا ، وليس في Builder Interface.

أحاول ببساطة دعم جميع توجهات الجهاز. يتم إنشاء جميع أقنعة التلقائي الخاصة بي ويتم تدوير كل شيء ويتوسع/العقود حسب الضرورة. كل شيء باستثناء NavigationController's Navbar and Toolbar. (عادةً ما تكون هذه المشاهدات تلقائيًا تلقائيًا حسب الاتجاه. أي في المشهد ، يجب أن يتم تغيير حجم شريط الأدوات وحرارة الأدوات تلقائيًا إلى حوالي 32 بكسل ، و 44 بكسل في وضع Portrain.) NAVBAR الفعلي وشريط الأدوات ليسوا كذلك. بمعنى آخر ، يمتد المحتوى/يتقلص حوالي 12 بكسل أو نحو ذلك في أي اتجاه كما لو أن شريط الأدوات وحجم شريط الأدوات. يبدو أن هذا هو الأقنعة التلقائية للقيام بعملهم.

لذلك لمحاولة حل هذا الموقف ، قمت بإنشاء فئة لـ UinavigationController التي تستجيب لـ "WillrotateToInterfaceorientation: المدة:" من أجل تشغيل محدد "sizetofit". (جاءت هذه التقنية من منشور آخر على Scack Overflow ، مع مشكلة مماثلة) بدوره يطلق عليه على وحدة تحكم NAV ، إلخ باستخدام تقنية sizetofit ، يتم تغيير حجم كل من شريط NAV وشريط الأدوات بمفردهما. ومع ذلك ، لا يزال ذلك يترك مسألة إعادة وضع شريط الأدوات ، من أجل تعويض الإزاحة من تغيير الحجم. (نظرًا لأن إحداثيات عرضنا في iPhone تبدأ في أعلى اليسار.)

ما يجعل هذا الأمر صعبًا في طريقة Willrotate ، هو أنه ليس لدينا أي طريقة لمعرفة أبعاد الرؤية المحيطة الجديدة ، ولا نعرف ما هو توجهنا الحالي. (ما لم يكن لديك تشغيل DeviceorientationNotifications ، وهو ما لا أفعله.) كل ما يتعين علينا العمل معه هو ما سيكون عليه الاتجاه الجديد ، وما هي قيم الإطار الحالية لشريط الأدوات لدينا. يمكنني أن أقوم بتدوين هذا وحساب تعديلات الإطار بالطريقة القديمة باستخدام قطعة من ورق الخدش ، لكنني أريد حقًا الاحتفاظ بميزات تغيير حجم العرض الديناميكي دون وضع أي افتراضات حول أبعاد شاشة الجهاز.

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

@implementation UINavigationController (BLUINavigationControllerAutoRotate)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return TRUE;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    // Resize the navBar
    [[self navigationBar] performSelector:@selector(sizeToFit)
                               withObject:nil
                               afterDelay:(0.5f * duration)];

    // Read our current frame size
    CGRect toolbarFrame = self.toolbar.frame;

    // Prevent over adjusting when flipping the device
    if ((toolbarFrame.size.height < 38
         && (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
             || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight))
        || (toolbarFrame.size.height > 38
            && (toInterfaceOrientation == UIInterfaceOrientationPortrait
                || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)))
    {
        // We are already resized and repositioned.
        return;
    }

    // Calculate and apply the toolbar offset
    const NSInteger delta = 12;     // the difference in size between 44 and 32 px.
    toolbarFrame.origin.y += ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
                              || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) ? (delta) : (-delta);
    self.toolbar.frame = toolbarFrame;

    // Resize the toolbar
    [[self toolbar] performSelector:@selector(sizeToFit)
                         withObject:nil
                         afterDelay:(0.5f * duration)];

}

@end

لذا ، لقد تصرفت الآن بتصرف في الملاحة وشريط الأدوات. لن يعلم المستخدم النهائي أبدًا أنه كان علي تنفيذ كل هذا العمل الإضافي من أجل إعادة تمكين ما يبدو أنه من المفترض أن يكون سلوكًا قياسيًا. لذا سؤالي هو ، لماذا يجب علي فعل كل هذا؟ هل هناك نهج أفضل يجب أن أعرفه ، أو شيء لا أفعله ببساطة في أي مكان آخر؟ (أي [NavigationController setautomallyricallyrisizestoolbaronrotate: صحيح] ؛ أو شيء من هذا القبيل؟) كما قلت ، يبدو أنه من المفترض أن يكون مسبقًا ، لذلك يجب أن أضطر إلى التوجيه مع إعادة توجيه الرسائل والمشغلات القسرية. بدلا من الحل المناسب.

شكرًا!

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

المحلول

أنت تمرر رسالة WillrotateToInterfaceorientation إلى وحدة التحكم في عرض علامة التبويب من وحدة التحكم في عرض الجذر - لماذا لا تمرر جميع رسائل الدوران الأخرى ومعرفة ما إذا كان ذلك يساعد؟ بمعنى آخر

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [tabBarController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [tabBarController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation  {
    [super didRotateFromInterfaceOrientation:toInterfaceOrientation];
    [tabBarController didRotateFromInterfaceOrientation:toInterfaceOrientation];
}

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [tabBarController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    [super didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
    [tabBarController didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
}

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateSecondHalfOfRotationFromInterfaceOrientation:toInterfaceOrientation duration:duration];
    [tabBarController willAnimateSecondHalfOfRotationFromInterfaceOrientation:toInterfaceOrientation duration:duration];
}

سأكون مهتمًا برؤية ما يحدث إذا قمت بتمرير كل هذه إلى وحدة تحكم شريط علامات التبويب - تبدو مشكلتك شيئًا يجب أن يحدث تلقائيًا ، بدلاً من أن تكون كل العمل الإضافي الذي قمت به!

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