iOS 10中rightBarButtonItem的错误位置

时间:2017-07-04 12:53:33

标签: ios swift uinavigationcontroller uinavigationbar

我想将view的{​​{1}}放入另一个视图控制器的UINavigationController。问题是iOS 10上的右侧栏按钮放错位置。在iOS 8和9上看起来很不错。

iOS 8和9:

enter image description here

iOS 10:

enter image description here

以下是我添加导航控制器的代码:

view

在MyViewController中

override func viewDidLoad() {
    super.viewDidLoad()

    let viewController = MyViewController()
    navController = UINavigationController(rootViewController: viewController)
    navController.willMove(toParentViewController: self)
    addChildViewController(navController)
    navController.view.frame = view.bounds
    view.addSubview(navController.view)
    navController.didMove(toParentViewController: self)
}

此外,没有剪切视图(按钮本身除外)。请参阅IB Debug View Hierachy的快照:

enter image description here

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我通过将import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import java.io.IOException; import java.util.List; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { @Override public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); LatLng latLng = new LatLng(latitude, longitude); Geocoder geocoder = new Geocoder(getApplicationContext()); try { List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); String str = addressList.get(0).getLocality(); str += addressList.get(0).getCountryName(); mMap.addMarker(new MarkerOptions().position(latLng).title(str)); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); } catch (IOException e) { e.printStackTrace(); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }); } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { @Override public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); LatLng latLng = new LatLng(latitude, longitude); Geocoder geocoder = new Geocoder(getApplicationContext()); try { List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); String str = addressList.get(0).getLocality(); str += addressList.get(0).getCountryName(); mMap.addMarker(new MarkerOptions().position(latLng).title(str)); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); } catch (IOException e) { e.printStackTrace(); } } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }); } } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { // mMap = googleMap; // mMap.getMaxZoomLevel(); // // Add a marker in Sydney and move the camera // LatLng posi = new LatLng(100, 100); // mMap.addMarker(new MarkerOptions().position(posi).title("my position")); // mMap.moveCamera(CameraUpdateFactory.newLatLng(posi)); } } viewWillAppear事件转发到导航控制器的根视图控制器来解决了这个问题。

viewDidAppear

答案 1 :(得分:0)

这种情况正在发生,因为您在ViewDidLoad中添加了子视图而不是viewDidAppear,请尝试更改此内容:

override func viewDidLoad() {
    super.viewDidLoad()


    let viewController = MyViewController()
    navController = UINavigationController(rootViewController: viewController)
    navController.willMove(toParentViewController: self)
    addChildViewController(navController)
    navController.view.frame = view.bounds
    view.addSubview(navController.view)
    navController.didMove(toParentViewController: self)
}

到此:

    let viewController = MyViewController()

    override func viewDidAppear() {
        super.viewDidAppear()
     if(self.childViewControllers.contains(viewController) != true){
        navController = UINavigationController(rootViewController: viewController)
        navController.willMove(toParentViewController: self)
        addChildViewController(navController)
        navController.view.frame = view.bounds
        view.addSubview(navController.view)
        navController.didMove(toParentViewController: self)

      }
 }