Wednesday, October 24, 2012

แก้ปัญหา Auto Rotate on iOS6

สำหรับ iOS6 SDK นั้นมีคำสั่งหลายตัวเริ่ม Deprecated ไปเหมือนกัน อย่างวันนี้จะเขียนเรื่องของ Autorotate บน iOS6 โดยใน iOS รุ่นต่ำกว่านี้เวลาที่เราต้องการสั่งให้เครื่องตั้งหรือนอน เราจะใช้คำสั่ง

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
}

แต่พอเป็น iOS6 คำสั่งนี้ได้ถูกยกเลิกไป แล้วเพิ่ม 2 คำสั่งนี้เข้ามาแทน

- (NSUInteger)supportedInterfaceOrientations
{
}

- (BOOL)shouldAutorotate
{ 
}

ดังนั้นสิ่งที่ผมจัดการกับปัญหานี้คือสร้าง Category ของ UINavigationController ขึ้นมาแล้วใน ViewController เราก็เรียกใช้คำสั่งเหล่านี้ได้เลย เช่นผมมีคลาส ViewController อยู่หนึ่งตัวก็จะเรียกใช้แบบนี้ได้เลย ตัวแรกจะใช้งานสำหรับ iOS 5 ลงไป

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

ส่วน 2 ตัวนี้จะใช้สำหรับ iOS 6 ขึ้นไปครับ

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

สำหรับ Category นี้ผมเขียนไว้แล้วลอง ดาวโหลด ไปใช้งานกันได้เลยครับ :D


No comments:

Post a Comment