任何人都可以幫助我如何使UILabel作爲NavigationBar的rightBarButtonItem?如何使UILabel作爲NavigationBar的rightBarButtonItem?
我試着用UILabel在UIView上做UIBarButtonItem的initWithCustomView,但崩潰了。
任何人都可以幫助我如何使UILabel作爲NavigationBar的rightBarButtonItem?如何使UILabel作爲NavigationBar的rightBarButtonItem?
我試着用UILabel在UIView上做UIBarButtonItem的initWithCustomView,但崩潰了。
你可以試試下面的代碼。
UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(260,6,55,32)];
yourLabel.text = @"Ashu";
[self.navigationController.navigationBar addSubview:yourLabel];
請告知,如果它的工作原理..
糾正一個括號後,它可以工作。 –
下面是一些示例代碼:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 65.0, 30.0)];
label.text = @"Text";
barButton = [[UIBarButtonItem alloc] initWithCustomView:label];
,那麼只需在設置「barButton」等於右導航項目
做到這一點的:
yourButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30) ];
[yourButton addTarget:self action:@selector(yourmethod:) forControlEvents:UIControlEventTouchUpInside];
yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 8, 40, 30)];
yourLabel.text = @"your Text";
yourLabel.backgroundColor = [UIColor clearColor];
yourLabel.textColor = [UIColor whiteColor];
yourLabel.font = [UIFont boldSystemFontOfSize:12.0f];
[yourLabel sizeToFit];
[yourButton addSubview:yourLabel];
[yourButton sizeToFit];
yourBarButton = [[UIBarButtonItem alloc] initWithCustomView:yourButton];
self.navigationItem.rightBarButtonItem = yourBarButton;
後的代碼,請。 –