2016-10-12 2 views
1

아래 검색 창을 추가하는 것은 내 코드입니다 : 여기탐색 제목 여기

[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, 500, 800)]; 
self.navigationItem.title = @"locations"; 
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.view.frame.size.width,110.0)]; 
UISearchBar *searchBar1 = [[UISearchBar alloc]initWithFrame:CGRectMake(10, 40, 240, 34)]; 
[self.navigationController.view addSubview:searchBar1]; 
elf.navigationItem.title = @"locations"; 
//self.navigationItem.titleView = searchController.searchBar; 
self.navigationController.navigationBar.barTintColor = `Color grayColor]; 
self.definesPresentationContext = YES; 

like that screen shot

+3

가능한 중복을 변경 [언더 검색 창을 추가하는 방법 탐색 제목] (http://stackoverflow.com/questions/39994506/how-to-add-search-bar-under-the-navigation-title) –

+1

왜 같은 질문을 두 번째? –

+0

출력물에 너무 가까이 갈 수 없습니다. 누구든지 나를 도와주세요. – ios

답변

10

유 완벽하게 이미지처럼 보이게하려는 경우를 go..Try u가 이전 질문에 게시되면 다음과 같이 할 수 있습니다.

[self.navigationController.navigationBar setTranslucent:NO]; 
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; 
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack]; 
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]]; 
[self.navigationController.navigationBar setShadowImage:[UIImage new]]; 

UISearchBar *searchBar = [[UISearchBar alloc] init]; 
searchBar.placeholder = @"search"; 
self.title = @"Locations"; 
searchBar.frame = CGRectMake(0, 0, self.navigationController.view.bounds.size.width, 64); 
searchBar.barStyle = UIBarStyleDefault; 
[searchBar setTranslucent:NO]; 
searchBar.barTintColor = [UIColor redColor]; 
searchBar.backgroundImage = [UIImage new]; 
[self.view addSubview:searchBar]; 

및 탐색 바는

enter image description here

빠른 코드

navigationController?.navigationBar.isTranslucent = false 
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    navigationController?.navigationBar.barStyle = .black 
    navigationController?.navigationBar.barTintColor = UIColor.red 
    navigationController?.navigationBar.shadowImage = UIImage() 
    title = "Location"; 


    let searchBar = UISearchBar() 
    searchBar.placeholder = "Search" 
    searchBar.frame = CGRect(x: 0, y: 0, width: (navigationController?.view.bounds.size.width)!, height: 64) 
    searchBar.barStyle = .default 
    searchBar.isTranslucent = false 
    searchBar.barTintColor = UIColor.red 
    searchBar.backgroundImage = UIImage() 
    view.addSubview(searchBar) 

스토리 보드 설치 아래와 같은 외모와 귀하의 요구 사항에 검색 창을 변경

모든 것을 괜찮아요, 그냥 설정해야합니다 스토리 보드보기 컨트롤러 t 오 embed in navigation controller (U이 아직없는 경우), 다음 뷰 컨트롤러에, 그냥 검색 창을 추가 한 후 다음을 수행 viewcontroller.swift에 대한 outlet을은의

@IBOutlet weak var searchBar: UISearchBar! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    //navigationController?.navigationBar.isTranslucent = false //set it in strory board 
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    //navigationController?.navigationBar.barStyle = .black ////set it in strory board 
    //navigationController?.navigationBar.barTintColor = UIColor.red ////set it in strory board 
    navigationController?.navigationBar.shadowImage = UIImage() 
    title = "Location"; 

    searchBar.barStyle = .default 
    searchBar.isTranslucent = false 
    searchBar.barTintColor = UIColor.red 
    searchBar.backgroundImage = UIImage() 
} 
+0

완벽하게 작동하지만 신속하게 해당 코드가 필요합니다. – ios

+0

스토리 보드에서 추가하고 색상을 변경할 수 있습니까? – ios

+0

코드가 작동했습니다. – ios

1

이 코드

self.navigationItem.title = @"locations"; 
UISearchBar *searchBar1 = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 34)]; 
[self.view addSubview:searchBar1]; 
NSLayoutConstraint *contTop = [NSLayoutConstraint constraintWithItem:searchBar1 
           attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom 
           multiplier:1.0 constant:0]; 

NSLayoutConstraint *consHeight = [NSLayoutConstraint constraintWithItem:searchBar1 attribute:NSLayoutAttributeHeight 
           relatedBy:NSLayoutRelationEqual toItem:nil 
           attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 
           constant:34]; 

NSLayoutConstraint *contLeading = [NSLayoutConstraint constraintWithItem:searchBar1 attribute:NSLayoutAttributeLeading 
            relatedBy:NSLayoutRelationEqual toItem:self.view 
            attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]; 
NSLayoutConstraint *conttrailing = [NSLayoutConstraint constraintWithItem:searchBar1 attribute:NSLayoutAttributeTrailing 
            relatedBy:NSLayoutRelationEqual toItem:self.view 
            attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]; 
[NSLayoutConstraint activateConstraints:@[contTop, consHeight,contLeading,conttrailing]]; 
[searchBar1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view layoutSubviews]; 
self.navigationItem.title = @"locations"; 
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor]; 
searchBar1.barTintColor= [UIColor orangeColor]; 
self.definesPresentationContext = YES; 
+0

탐색 테두리 색상 – ios

+0

[self.navigationController.navigationBar.layer setBorderWidth : 2.0] 변경하는 방법; // 그냥 확인하기 위해 그것의 작업 [self.navigationController.navigationBar.layer setBorderColor : [[UIColor whiteColor] CGColor]]; 검색 막대의 테두리 색상을 같은 방법으로 설정할 수 있습니다. – Darshana

관련 문제