좋아,이 작업을 성공적으로 수행하는 데 어려움을 겪고 있습니다. 기본적으로 UIView에 UILabel을 추가하고 가운데에 배치하려고합니다.UILabel의 SetCenter 문제
코드는 다음과 같습니다
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
내가 UILabel의가있는 UIView 내에서 중심 것이라고 예상했다. 글쎄, 그건 아니야. 그것은 어떻게 든 오른쪽 정렬 및 UIView의 중심에 가깝지 않습니다.
내가 뭘 잘못하고 있니?
방금 귀하의 코드를 사용해 보았습니다. 그리고 저를 위해 올바르게 작동하고 있습니다 (레이블은 올바르게보기 중심에 위치 함). 아마도 다른 뭔가가 그것을 방해하고 있을까요? – jnic
Ooops, 사용자 정의 UIView가 상단 네비게이션 바에서 titleView 속성을 대체한다는 점을 잊어 버렸습니다. 나는 게시 된 코드를 업데이트 할 것이다. – TeodorC
시도해보십시오 'UILabel * titleLabel = [[UILabel 할당] initWithFrame : CGRectMake (customTitleView.frame.size.width/2 크기. 폭/2, customTitleView.frame.size.height/2 크기. 높이/2, size.width, size.height)] ' 제목 라벨을 초기화하려면 – Seega