【发布时间】:2018-07-26 00:14:49
【问题描述】:
我尝试将UISegmentedControl 添加到UINavigationBar,但在运行时,UISegmentedControl 未显示。
这是一个代码
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
return YES;
}
MainViewController2.m
#import "MainViewController2.h"
@interface MainViewController2 ()
@end
@implementation MainViewController2
@synthesize firstVC;
@synthesize segment;
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
segment.frame =CGRectMake(10,100,199,100);
segment = [[UISegmentedControl alloc]initWithItems:@[@"seg1", @"seg2", @"seg3"]];
self.navigationItem.titleView = segment;
[self.view addSubview:firstVC];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
【问题讨论】:
-
您查看的是嵌入在导航控制器中的控制器吗?
-
self.firstVC.view是什么,为什么要在其中添加它?您的 viewController 是否嵌入在 navigationController 中? -
这是默认的 Viewcontroller ,我要显示
-
你的意思是rootviewronroller必须是navigatrionController?现在我使用 UIViewController。
-
显示您的
AppDelegate.m代码
标签: ios objective-c xcode uiviewcontroller uinavigationbar