ฉันได้คลาสย่อย UINavigationBar เพื่อให้มีแถบการนำทางที่กำหนดเองภายในแอปพลิเคชันของฉัน แถบนี้ถูกนำมาใช้ซ้ำบนตัวควบคุมหลายมุมมองที่มีปุ่มเมนูสไตล์เดียวกันกับรายการปุ่มด้านซ้าย ซึ่งเป็นสาเหตุที่ทำให้มีคลาสย่อย
จากนั้นคลาสย่อยจะถูกเพิ่มลงในแถบการนำทาง View Controllers ในกระดานเรื่องราว
นี่คือรหัสภายในคลาสย่อย UINavigationBar
:
- (void)drawRect:(CGRect)rect
{
// Drawing code
UIImage *image = [[UIImage imageNamed: @"mainNavBar"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5,0,5)];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
-(void)awakeFromNib {
UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(7.0f, 7.0f, 38.0f, 29.0f)];
[leftButton setImage:[UIImage imageNamed:@"menuBarItem"] forState:UIControlStateNormal];
[leftButton addTarget:nil action:@selector(menuItemPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftButton];
}
ปัญหาคือ การหมุนของอุปกรณ์ ฉันยังมีโค้ดบางส่วนใน appDidFinishLaunching
โดยใช้ API ลักษณะที่ปรากฏสำหรับการตั้งค่าเพิ่มเติม:
// Custom Navigation Bar appearance setup
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,nil]];
// Used to deal with the rotation of the nav bar when implemented outside of Navigation Controller
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight];
[[UINavigationBar appearance] setContentMode:UIViewContentModeScaleAspectFit];
// Used to push the title down slightly when in Landscape and NavBar outside of Navigation Controller
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2 forBarMetrics:UIBarMetricsLandscapePhone];
เมื่ออุปกรณ์หมุน อุปกรณ์ทั้งหมดจะทำงานได้อย่างถูกต้องในแง่ของความสูงของแถบตามที่คาดไว้ตั้งแต่ 44 ถึง 32px อย่างไรก็ตาม ปุ่มต่างๆ ยังคงปรากฏอยู่ด้านนอกของแถบ
ฉันได้ดูโพสต์ SO อื่น ๆ เกี่ยวกับเรื่องนี้แล้ว แต่ไม่สามารถหาวิธีดำเนินการให้ถูกต้องได้: iPhone: UINavigationBar พร้อมปุ่ม - ปรับความสูง
UINavigationBar ปรับขนาดอัตโนมัติ
ตามหลักการแล้ว ฉันไม่ต้องการจัดการกับการหมุนอัตโนมัติใน View Controller เนื่องจาก UINavigationBar นี้ถูกนำมาใช้ซ้ำผ่าน VC จำนวนมาก การเพิ่มรหัสการหมุนเวียนอัตโนมัติหมายความว่า VC แต่ละคนอาจต้องการสิ่งนี้ด้วยหรือไม่
แก้ไข - คำตอบต่อไปนี้หากฉันเปลี่ยน awakeFromNib
เพื่อรวมสิ่งต่อไปนี้:
UINavigationItem* ni = [[UINavigationItem alloc] initWithTitle:@"Test"];
UIImage *menuBgImage = [UIImage imageNamed:@"menuBarItem"];
UIBarButtonItem *b =[[UIBarButtonItem alloc] initWithImage:menuBgImage landscapeImagePhone:menuBgImage style:UIBarButtonItemStylePlain target:nil action:@selector(menuItemPressed:)];
ni.leftBarButtonItem = b;
self.items = @[ni];
ฉันมีปัญหาที่ UIBarButtonItemStylePlain ทำลายรูปภาพ:
หากฉันตั้งค่าก่อนหน้าด้วย initWithCustomView และใช้ปุ่มซ้ายจากโค้ดต้นฉบับ ด้วยปุ่มซ้ายที่ใช้การปรับขนาดอัตโนมัติสำหรับความสูง - ฉันจึงได้ภาพที่ยืดออกไม่ดีในแนวนอนใช่ไหม
แก้ไข 2 - รูปภาพเพิ่มเติมต่อคำตอบ